├── HumanEval.jsonl.gz
├── requirements.txt
├── README.md
├── human_eval.py
├── roy.py
└── quickstart.ipynb
/HumanEval.jsonl.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JosefAlbers/Roy/HEAD/HumanEval.jsonl.gz
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | accelerate
2 | auto-gptq
3 | faiss-cpu
4 | transformers
5 | optimum
6 | torch
7 | prompt-toolkit
8 | pandas
9 | tqdm
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Roy
2 |
3 | [
](https://colab.research.google.com/github/JosefAlbers/Roy/blob/main/quickstart.ipynb)
4 | [](https://zenodo.org/badge/latestdoi/699801819)
5 |
6 | Roy is a lightweight alternative to `autogen` for developing advanced multi-agent systems using language models. It aims to simplify and democratize the development of emergent collective intelligence.
7 |
8 | ## Features
9 |
10 | - **Model Agnostic**: Use any LLM, no external APIs required. Defaults to a 4-bit quantized wizard-coder-python model for efficiency.
11 |
12 | - **Modular and Composable**: Roy decomposes agent interactions into reusable building blocks - templating, retrieving, generating, executing.
13 |
14 | - **Transparent and Customizable**: Every method has a clear purpose. Easily swap out components or add new capabilities.
15 |
16 | ## Quickstart
17 |
18 | ```sh
19 | git clone https://github.com/JosefAlbers/Roy
20 | cd Roy
21 | pip install -r requirements.txt
22 | pip install -U transformers optimum accelerate auto-gptq --extra-index-url https://huggingface.github.io/autogptq-index/whl/cu118/
23 | ```
24 |
25 | ```python
26 | from roy import Roy
27 | roy = Roy()
28 | ```
29 |
30 | ### **Template-Based Generation**
31 |
32 | Use templates to structure conversations and provide context.
33 |
34 | ```python
35 | s = 'What date is today? Which big tech stock has the largest year-to-date gain this year? How much is the gain?'
36 | roy.generate(roy.format(s))
37 | ```
38 |
39 | ### **Retrieval Augmented Generation**
40 |
41 | Enhance generation with relevant knowledge.
42 |
43 | ```python
44 | s = 'Create a text to image generator.'
45 | r = roy.retrieve(s, n_topk=3, src='huggingface')
46 | [roy.generate(s) for s in r]
47 | ```
48 |
49 | ### **Auto-Feedback**
50 |
51 | Agents collaborate in tight loops to iteratively refine outputs to specification.
52 |
53 | ```python
54 | # Iterative multiturn chat style
55 | s = "Create a secure and unique secret code word with a Python script that involves multiple steps to ensure the highest level of confidentiality and protection.\n"
56 | for i in range(2):
57 | c = roy.generate(s, prohibitions=['input'])
58 | s += roy.execute(c)
59 | ```
60 |
61 | ### **Rapid Benchmarking**
62 |
63 | Easily benchmark and iterate on your model architecture:
64 |
65 | - **Swap Components**: Language models, prompt formats, agent architectures etc.
66 |
67 | - **Test on Diverse Tasks**: Arithmetic, python coding, OpenAI's HumanEval etc.
68 |
69 | - **Quantify Improvements**: See how each change affects overall performance.
70 |
71 | ```python
72 | from human_eval import evaluate
73 | evaluate(roy.generate)
74 | ```
75 |
76 | ### **Emergent Multi-Agent Dynamics**
77 |
78 | Roy aims to facilitate the emergence of complex, adaptive multi-agent systems. It draws inspiration from biological and AI concepts to enable decentralized coordination and continual learning.
79 |
80 | - **Survival of the Fittest** - Periodically evaluate and selectively retain high-performing agents based on accuracy, speed etc. Agents adapt through peer interactions.
81 |
82 | - **Mixture of Experts** - Designate agent expertise, dynamically assemble specialist teams, and route tasks to optimal experts. Continuously refine and augment experts.
83 |
84 | These mechanisms facilitate the emergence of capable, adaptive, and efficient agent collectives.
85 |
86 | Flexible primitives to build ecosystems of agents.
87 |
88 | ```python
89 | from roy import Roys
90 | roys = Roys()
91 |
92 | # AutoFeedback
93 | roys.create(agents = {'Coder': 'i = execute(generate(i))'})
94 | roys.start(requests = {'i': 'Create a mobile application that can track the health of elderly people living alone in rural areas.'})
95 |
96 | # Retrieval Augmented Generation
97 | roys.create(
98 | agents = {
99 | 'Retriever': 'r = retrieve(i)',
100 | 'Generator': 'o = generate(r)',
101 | })
102 | roys.start(requests = {'i': 'Create a Deutsch to English translator.'})
103 |
104 | # Providing a custom tool to one of the agents using lambda
105 | roys.create(
106 | agents = {
107 | 'Coder': 'c = generate(i)',
108 | 'Proxy': 'c = custom(execute(c))',
109 | },
110 | tools = {'custom': lambda x:f'Modify the code to address the error encountered:\n\n{x}' if 'Error' in x else None})
111 | roys.start(requests = {'i': 'Compare the year-to-date gain for META and TESLA.'})
112 | ```
113 |
114 | ## Get Involved
115 |
116 | Roy is under active development. We welcome contributions - feel free to open issues and PRs!
117 |
118 | ## Support the Project
119 |
120 | If you found this project helpful or interesting and want to support more of these experiments, feel free to buy me a coffee!
121 |
122 |
123 |
--------------------------------------------------------------------------------
/human_eval.py:
--------------------------------------------------------------------------------
1 | # This file contains codes adapted from:
2 | # - abacaj's code-eval (https://github.com/abacaj/code-eval)
3 | # - OpenAI's human-eval (https://github.com/openai/human-eval)
4 |
5 | # Copyright (c) abacaj
6 | # Licensed under The MIT License (https://github.com/abacaj/code-eval/blob/main/LICENSE)
7 | # Copyright (c) OpenAI
8 | # Licensed under The MIT License (https://github.com/openai/human-eval)
9 |
10 | import glob
11 | import torch
12 | import gzip
13 | import json
14 | from tqdm.auto import tqdm
15 | import re
16 | from collections import defaultdict, Counter
17 | from concurrent.futures import ThreadPoolExecutor, as_completed
18 | from typing import List, Union, Iterable, Dict
19 | import itertools
20 | import numpy as np
21 | from typing import Iterable, Optional, Callable, Dict
22 | import ast
23 | import contextlib
24 | import faulthandler
25 | import io
26 | import os
27 | import multiprocessing
28 | import platform
29 | import signal
30 | import tempfile
31 |
32 | CWD = os.getcwd()
33 |
34 | def read_problems(evalset_file):
35 | return {task["task_id"]: task for task in stream_jsonl(evalset_file)}
36 |
37 | def stream_jsonl(filename: str) -> Iterable[Dict]:
38 | """
39 | Parses each jsonl line and yields it as a dictionary
40 | """
41 | if filename.endswith(".gz"):
42 | with open(filename, "rb") as gzfp:
43 | with gzip.open(gzfp, 'rt') as fp:
44 | for line in fp:
45 | if any(not x.isspace() for x in line):
46 | yield json.loads(line)
47 | else:
48 | with open(filename, "r") as fp:
49 | for line in fp:
50 | if any(not x.isspace() for x in line):
51 | yield json.loads(line)
52 |
53 | def write_jsonl(filename: str, data: Iterable[Dict], append: bool = False):
54 | """
55 | Writes an iterable of dictionaries to jsonl
56 | """
57 | if append:
58 | mode = 'ab'
59 | else:
60 | mode = 'wb'
61 | filename = os.path.expanduser(filename)
62 | if filename.endswith(".gz"):
63 | with open(filename, mode) as fp:
64 | with gzip.GzipFile(fileobj=fp, mode='wb') as gzfp:
65 | for x in data:
66 | gzfp.write((json.dumps(x) + "\n").encode('utf-8'))
67 | else:
68 | with open(filename, mode) as fp:
69 | for x in data:
70 | fp.write((json.dumps(x) + "\n").encode('utf-8'))
71 |
72 | def custom_sort_key(key):
73 | parts = key.split('/')
74 | return (parts[0], int(parts[1]))
75 |
76 | def generate_raw(fx, debug, eval_file):
77 | out_path = os.path.join(CWD, 'generated.jsonl')
78 | problems = read_problems(eval_file)
79 | samples = []
80 | pbar = tqdm(total=len(problems))
81 |
82 | sorted_keys = sorted(problems.keys(), key=custom_sort_key)
83 | if debug is not None:
84 | numerator, denominator = debug
85 | sublists_idx = [sorted_keys[i:i + len(sorted_keys)//denominator] for i in range(0, len(sorted_keys), len(sorted_keys)//denominator)]
86 | list_id = sublists_idx[numerator]
87 | else:
88 | list_id = sorted_keys
89 |
90 | for task_id in list_id:
91 | print(task_id)
92 | prompt = problems[task_id]["prompt"]
93 |
94 | batch_completions = [fx(prompt)]
95 |
96 | for sample in batch_completions:
97 | result = dict(
98 | task_id=task_id,
99 | completion=sample,
100 | )
101 |
102 | samples += [result]
103 | pbar.update(1)
104 |
105 | write_jsonl(out_path, samples)
106 | return out_path
107 |
108 |
109 | def extract_code(eval_file):
110 |
111 | in_path = os.path.join(CWD, 'generated.jsonl')
112 | out_path = os.path.join(CWD, 'extracted.jsonl')
113 |
114 | problems = read_problems(eval_file)
115 |
116 | output = []
117 | a = 0
118 | codes = [c for c in stream_jsonl(in_path)]
119 | for code in codes:
120 | task_id = code["task_id"]
121 | prompt = problems[task_id]["prompt"]
122 | completion = code["completion"]
123 | completion = completion.replace("\r", "")
124 | if "```python" in completion:
125 | def_line = completion.index("```python")
126 | completion = completion[def_line:].strip()
127 | completion = completion.replace("```python", "")
128 | try:
129 | next_line = completion.index("```")
130 | completion = completion[:next_line].strip()
131 | except:
132 | a += 1
133 | print(completion)
134 | print("================\n")
135 | if '__name__ == "__main__"' in completion:
136 | next_line = completion.index('if __name__ == "__main__":')
137 | completion = completion[:next_line].strip()
138 |
139 | if "# Example usage" in completion:
140 | next_line = completion.index("# Example usage")
141 | completion = completion[:next_line].strip()
142 |
143 | code["completion"] = completion
144 |
145 | output += codes
146 |
147 | write_jsonl(out_path, output)
148 | print(a)
149 | return out_path
150 |
151 | def check_correctness(problem: Dict, completion: str, timeout: float,
152 | completion_id: Optional[int] = None) -> Dict:
153 | """
154 | Evaluates the functional correctness of a completion by running the test
155 | suite provided in the problem.
156 |
157 | :param completion_id: an optional completion ID so we can match
158 | the results later even if execution finishes asynchronously.
159 | """
160 |
161 | def unsafe_execute():
162 |
163 | with create_tempdir():
164 |
165 | # These system calls are needed when cleaning up tempdir.
166 | import os
167 | import shutil
168 | rmtree = shutil.rmtree
169 | rmdir = os.rmdir
170 | chdir = os.chdir
171 |
172 | # Construct the check program and run it.
173 | check_program = (
174 | problem["prompt"] + '\n' + completion + "\n" +
175 | # completion + '\n' +
176 | problem["test"] + "\n" +
177 | f"check({problem['entry_point']})"
178 | )
179 | print(check_program)
180 |
181 | try:
182 | exec_globals = {}
183 | with swallow_io():
184 | with time_limit(timeout):
185 | print(exec(check_program, exec_globals))
186 | print('PASS')
187 | result.append("passed")
188 | except TimeoutException:
189 | print('TIMEOUT')
190 | result.append("timed out")
191 | except BaseException as e:
192 | print('FAIL')
193 | print(e)
194 | result.append(f"failed: {e}")
195 |
196 | # Needed for cleaning up.
197 | shutil.rmtree = rmtree
198 | os.rmdir = rmdir
199 | os.chdir = chdir
200 |
201 | manager = multiprocessing.Manager()
202 | result = manager.list()
203 |
204 | p = multiprocessing.Process(target=unsafe_execute)
205 | p.start()
206 | p.join(timeout=timeout + 1)
207 | if p.is_alive():
208 | p.kill()
209 |
210 | if not result:
211 | result.append("timed out")
212 |
213 | return dict(
214 | task_id=problem["task_id"],
215 | passed=result[0] == "passed",
216 | result=result[0],
217 | completion_id=completion_id,
218 | )
219 |
220 |
221 | @contextlib.contextmanager
222 | def time_limit(seconds: float):
223 | def signal_handler(signum, frame):
224 | raise TimeoutException("Timed out!")
225 | signal.setitimer(signal.ITIMER_REAL, seconds)
226 | signal.signal(signal.SIGALRM, signal_handler)
227 | try:
228 | yield
229 | finally:
230 | signal.setitimer(signal.ITIMER_REAL, 0)
231 |
232 |
233 | @contextlib.contextmanager
234 | def swallow_io():
235 | stream = WriteOnlyStringIO()
236 | with contextlib.redirect_stdout(stream):
237 | with contextlib.redirect_stderr(stream):
238 | with redirect_stdin(stream):
239 | yield
240 |
241 | @contextlib.contextmanager
242 | def create_tempdir():
243 | with tempfile.TemporaryDirectory() as dirname:
244 | with chdir(dirname):
245 | yield dirname
246 |
247 | class TimeoutException(Exception):
248 | pass
249 |
250 | class WriteOnlyStringIO(io.StringIO):
251 | """ StringIO that throws an exception when it's read from """
252 |
253 | def read(self, *args, **kwargs):
254 | raise IOError
255 |
256 | def readline(self, *args, **kwargs):
257 | raise IOError
258 |
259 | def readlines(self, *args, **kwargs):
260 | raise IOError
261 |
262 | def readable(self, *args, **kwargs):
263 | """ Returns True if the IO object can be read. """
264 | return False
265 |
266 |
267 | class redirect_stdin(contextlib._RedirectStream): # type: ignore
268 | _stream = 'stdin'
269 |
270 | @contextlib.contextmanager
271 | def chdir(root):
272 | if root == ".":
273 | yield
274 | return
275 | cwd = os.getcwd()
276 | os.chdir(root)
277 | try:
278 | yield
279 | except BaseException as exc:
280 | raise exc
281 | finally:
282 | os.chdir(cwd)
283 |
284 | def estimate_pass_at_k(
285 | num_samples: Union[int, List[int], np.ndarray],
286 | num_correct: Union[List[int], np.ndarray],
287 | k: int
288 | ) -> np.ndarray:
289 | """
290 | Estimates pass@k of each problem and returns them in an array.
291 | """
292 |
293 | def estimator(n: int, c: int, k: int) -> float:
294 | """
295 | Calculates 1 - comb(n - c, k) / comb(n, k).
296 | """
297 | if n - c < k:
298 | return 1.0
299 | return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1, n + 1))
300 |
301 | if isinstance(num_samples, int):
302 | num_samples_it = itertools.repeat(num_samples, len(num_correct))
303 | else:
304 | assert len(num_samples) == len(num_correct)
305 | num_samples_it = iter(num_samples)
306 |
307 | return np.array([estimator(int(n), int(c), k) for n, c in zip(num_samples_it, num_correct)])
308 |
309 |
310 | def evaluate_functional_correctness(
311 | problem_file,
312 | k = [1],
313 | n_workers = 1,
314 | timeout = 5.0,
315 | ):
316 | """
317 | Evaluates the functional correctness of generated samples, and writes
318 | results to f"{sample_file}_results.jsonl.gz"
319 | """
320 | sample_file = os.path.join(CWD, 'extracted.jsonl')
321 | problems = read_problems(problem_file)
322 |
323 | # Check the generated samples against test suites.
324 | with ThreadPoolExecutor(max_workers=n_workers) as executor:
325 |
326 | futures = []
327 | completion_id = Counter()
328 | n_samples = 0
329 | results = defaultdict(list)
330 |
331 | print("Reading samples...")
332 | for sample in tqdm(stream_jsonl(sample_file)):
333 | task_id = sample["task_id"]
334 | completion = sample["completion"]
335 | args = (problems[task_id], completion, timeout, completion_id[task_id])
336 | future = executor.submit(check_correctness, *args)
337 | futures.append(future)
338 | completion_id[task_id] += 1
339 | n_samples += 1
340 |
341 | # assert len(completion_id) == len(problems), "Some problems are not attempted."
342 |
343 | print("Running test suites...")
344 | for future in tqdm(as_completed(futures), total=len(futures)):
345 | result = future.result()
346 | results[result["task_id"]].append((result["completion_id"], result))
347 |
348 | # Calculate pass@k.
349 | total, correct = [], []
350 | for result in results.values():
351 | result.sort()
352 | passed = [r[1]["passed"] for r in result]
353 | total.append(len(passed))
354 | correct.append(sum(passed))
355 | total = np.array(total)
356 | correct = np.array(correct)
357 |
358 | ks = k
359 | pass_at_k = {f"pass@{k}": estimate_pass_at_k(total, correct, k).mean()
360 | for k in ks if (total >= k).all()}
361 |
362 | # Finally, save the results in one file:
363 | def combine_results():
364 | for sample in stream_jsonl(sample_file):
365 | task_id = sample["task_id"]
366 | result = results[task_id].pop(0)
367 | sample["result"] = result[1]["result"]
368 | sample["passed"] = result[1]["passed"]
369 | yield sample
370 |
371 | out_file = sample_file + "_results.jsonl"
372 | print(f"Writing results to {out_file}...")
373 | write_jsonl(out_file, tqdm(combine_results(), total=n_samples))
374 | print(f'{pass_at_k=}')
375 | print(f'{total=}')
376 | print(f'{correct=}')
377 |
378 | correct_file = 'correct.npy'
379 | np.save(correct_file, correct)
380 |
381 | return out_file, correct_file
382 |
383 | def evaluate(fx, debug=None, eval_file='HumanEval.jsonl.gz'):
384 | raw_file = generate_raw(fx, debug=debug, eval_file=eval_file)
385 | code_file = extract_code(eval_file=eval_file)
386 | result_file, correct_file = evaluate_functional_correctness(problem_file=eval_file)
387 | return [raw_file, code_file, result_file, correct_file]
388 |
389 |
--------------------------------------------------------------------------------
/roy.py:
--------------------------------------------------------------------------------
1 | import os
2 | os.environ["TOKENIZERS_PARALLELISM"] = "false"
3 | import sys
4 | import torch
5 | from torch.nn.utils.rnn import pad_sequence
6 | from transformers import AutoModelForCausalLM, AutoModel, AutoTokenizer
7 | from huggingface_hub import hf_hub_download
8 | import faiss
9 | import pandas as pd
10 | import numpy as np
11 | from textwrap import indent, dedent
12 | import re
13 | import subprocess
14 | import shlex
15 | import venv
16 | from prompt_toolkit import PromptSession
17 | from prompt_toolkit.key_binding import KeyBindings
18 | from prompt_toolkit.lexers import PygmentsLexer
19 | from prompt_toolkit.styles import Style
20 | from pygments.lexers.python import Python3Lexer
21 | import argparse
22 | from tqdm.auto import tqdm
23 | from io import StringIO
24 | from datetime import datetime, timedelta
25 | import copy
26 | import types
27 | import inspect
28 |
29 | LOG_LEVEL = 5
30 | log_buffer = StringIO()
31 |
32 | def get_timestamp():
33 | dt = datetime.utcnow() + timedelta(hours=9)
34 | return dt.strftime('%Y%m%d%H%M%S')
35 |
36 | def log(s, log_level=5):
37 | if log_level < LOG_LEVEL+1:
38 | log_message = f'\n{get_timestamp()}\n\033[{31+log_level}m\n{s}\n\033[0m\n'
39 | print(log_message)
40 | log_buffer.write(log_message)
41 |
42 | def dump_log(log_file="log.txt"):
43 | with open(log_file, "a") as file:
44 | file.write(log_buffer.getvalue())
45 | log_buffer.truncate(0)
46 | return log_file
47 |
48 |
49 | def trace_method(func):
50 | def wrapper(*args, **kwargs):
51 | result = func(*args, **kwargs)
52 | log_in = '\n'.join(str(i) for i in args)
53 | log(f"{func.__name__}() receives:\n{indent(log_in, ' ')}", 3)
54 | log_out = '\n'.join(str(i) for i in result) if isinstance(result, (list, tuple)) else str(result)
55 | log(f"{func.__name__}() returns:\n{indent(log_out, ' ')}", 2)
56 | return result
57 | return wrapper
58 |
59 | def process_code_string(s):
60 | if '>>>' not in s:
61 | return s
62 | def replace_line_prefix(match):
63 | prefix = match.group(1)
64 | if prefix in [">>> ", "... "]:
65 | return ""
66 | return "# " + match.group(0)
67 | pattern = r"^(>>> |... |\S+.*$)"
68 | return re.sub(pattern, replace_line_prefix, s, flags=re.MULTILINE)
69 |
70 | def extract_code_block(s, is_python):
71 | s = s.replace('\r', '')
72 | pattern = r'```(?:\s*(\w+?)\s*\n)?(.*?)```'
73 | matches = re.findall(pattern, s, re.DOTALL)
74 | if len(matches) < 1:
75 | return ''
76 | code = ''
77 | for m in matches:
78 | is_python = identify_lang(m) if is_python is None else is_python
79 | code += m[1] if is_python else re.sub(r'^(?![!])', '!', m[1], flags=re.MULTILINE)
80 | return code.rstrip()
81 |
82 | def process_markdown_data(df):
83 | df = df[~df['filepath'].str.contains('/zh/')]
84 | df['filepath'] = df['filepath'].str[7:]
85 | df['content'] = df['content'].str[:5000]
86 | df['retrieved_content'] = df.apply(lambda row: f"{row['filepath'].split('/')[-1]} ({row['filepath']}):\n'''\n{row['content']}...\n'''", axis=1)
87 | return df
88 |
89 | def process_docstr_data(df):
90 | def truncate_string(row, char_limit, variable_str, constant_str):
91 | if not (isinstance(row[variable_str], str) and isinstance(row[constant_str], str)):
92 | return ""
93 | if len(row[constant_str]) >= char_limit:
94 | return ""
95 | trimmed_length = char_limit - len(row[constant_str])
96 | return row[variable_str][:trimmed_length]
97 | df = df[df['docstring'].str.contains('```')]
98 | df = df[~df['filepath'].apply(lambda x: x.split('/')[-1]).str.startswith('TF')]
99 | df.reset_index(drop=True, inplace=True)
100 | df['filepath'] = df['filepath'].str[7:].str.rstrip('/.')
101 | df['root_dir'] = df['filepath'].apply(lambda x: x.split('/')[0])
102 | df['retrieved_code'] = df['docstring'].apply(extract_code_block, args=(True,)).apply(process_code_string)
103 | df['docstring'] = df.apply(truncate_string, args=(5000,'docstring','retrieved_code'), axis=1)
104 | df['retrieved_docstr'] = df.apply(lambda row: f"{row['type']} `{row['filepath'].split('/')[-1]}` ({row['filepath']}):\n'''\n{row['docstring']}...\n'''", axis=1)
105 | return df
106 |
107 | def edit_code_in_terminal(initial_text):
108 | kb = KeyBindings()
109 | result = {'text': initial_text}
110 | @kb.add('s-tab')
111 | def _(event):
112 | result['text'] = event.app.current_buffer.text
113 | event.app.exit()
114 | style = Style.from_dict({
115 | '': '#ffad00',
116 | 'prompt': 'bg:#ff0000 #ffff00',
117 | })
118 | session = PromptSession(lexer=PygmentsLexer(Python3Lexer), key_bindings=kb, style=style)
119 | session.prompt('\n--- Press shift+tab when done ---\n', multiline=True, default=initial_text)
120 | result_text = result['text']
121 | return result_text
122 |
123 | def identify_lang(match): # stub
124 | if 'py' in match[0]:
125 | is_python = True
126 | elif 'sh' in match[0]:
127 | is_python = False
128 | else:
129 | if '!pip install ' in match[1]:
130 | is_python = True
131 | elif 'pip install ' in match[1]:
132 | is_python = False
133 | else:
134 | log('Unable to identify code language')
135 | is_python = True
136 | return is_python
137 |
138 | class VirtualEnvironment:
139 | def __init__(self, time_limit=20, venv_path='venvRoy'):
140 | self.venv_path = venv_path
141 | self.time_limit = time_limit
142 | try:
143 | if not os.path.exists(self.venv_path):
144 | venv.EnvBuilder(with_pip=True).create(self.venv_path)
145 | if os.name == 'nt':
146 | self.python_executable = os.path.join(venv_path, "Scripts", "python.exe")
147 | self.pip_executable = os.path.join(venv_path, "Scripts", "pip.exe")
148 | else:
149 | self.python_executable = os.path.join(venv_path, "bin", "python")
150 | self.pip_executable = os.path.join(venv_path, "bin", "pip")
151 | subprocess.run(f'{self.python_executable} -V')
152 | subprocess.run(f'{self.pip_executable} -V')
153 | except:
154 | log("Warning: Failed to create or locate virtual environment. Using default system python and pip.")
155 | self.python_executable = "python"
156 | self.pip_executable = "pip"
157 |
158 | def _run_cmd(self, command):
159 | replacements = {
160 | "python": self.python_executable,
161 | "pip": self.pip_executable,
162 | "pip3": self.pip_executable,
163 | }
164 | command_parts = shlex.split(command)
165 | command_parts = [replacements.get(part, part) for part in command_parts]
166 | adjusted_command = ' '.join(shlex.quote(part) for part in command_parts)
167 |
168 | try:
169 | output = subprocess.run(
170 | adjusted_command,
171 | shell=True,
172 | check=True,
173 | stdout=subprocess.PIPE,
174 | stderr=subprocess.STDOUT,
175 | timeout=self.time_limit,
176 | ).stdout.decode()
177 | except subprocess.TimeoutExpired:
178 | output = "TimeoutError: Execution Exceeded Time Limit (Suspected Infinite Loop)"
179 | except subprocess.CalledProcessError as error:
180 | output = str(error.stdout.decode()).strip()
181 | return output
182 |
183 | def _run(self, code_string, script_name="script.py"):
184 | code_string = code_string.rstrip()
185 | ls = re.findall(r'^!(.*)$', code_string, re.MULTILINE)
186 | code_string = re.sub(r'^(!)', r'#\1', code_string, flags=re.MULTILINE)
187 | with open(script_name, 'w', encoding='utf-8') as f:
188 | f.write(code_string)
189 | ls.append(f"python {script_name}")
190 | return '\n'.join([self._run_cmd(s) for s in ls]).rstrip()
191 |
192 | def execute(self, s, is_python=None, join=True):
193 | x_in = extract_code_block(s, is_python)
194 | x_out = self._run(x_in)
195 | if join is True:
196 | return '[Code]:\n```python\n{x_in}\n```\n\n[Output]:\n```\n{x_out}\n```\n'.format(x_in=x_in, x_out=x_out)
197 | return f'```python\n{x_in}\n```', f'```\n{x_out}\n```'
198 |
199 | class RM:
200 | def __init__(self, configs=None, model_id="BAAI/bge-small-en", query_instruction='Represent this sentence for searching relevant passages: '):
201 | default_config_for_RM = {
202 | 'markdown': {
203 | 'filename_key': 'hfmd_20230927192215',
204 | 'process_data': process_markdown_data
205 | },
206 | 'huggingface': {
207 | 'filename_key': 'hfds_20230927191331',
208 | 'process_data': process_docstr_data
209 | },
210 | }
211 | self.configs = default_config_for_RM if configs is None else configs
212 | self.resources = {}
213 | for src, config in self.configs.items():
214 | self._init_filenames(src)
215 | self._load_resources(src)
216 | self._init_model(model_id, query_instruction)
217 |
218 | def _init_filenames(self, src):
219 | config = self.configs[src]
220 | filename_key = config['filename_key']
221 | fn_index = f'index_{filename_key}.index'
222 | fn_df = f'df_{filename_key}.csv'
223 | self.resources[src] = {
224 | "fn_index": fn_index,
225 | "fn_df": fn_df
226 | }
227 |
228 | def _load_resources(self, src):
229 | res = self.resources[src]
230 | for fn_i in [res["fn_index"], res["fn_df"]]:
231 | hf_hub_download(
232 | repo_id="Accede/vecDB",
233 | filename=fn_i,
234 | repo_type='dataset',
235 | local_dir='.'
236 | )
237 | res["index"] = faiss.read_index(res["fn_index"])
238 | res["df"] = pd.read_csv(res["fn_df"])
239 |
240 | def _init_model(self, model_id, query_instruction):
241 | self.QUERY_INST = query_instruction
242 | self.tokenizer = AutoTokenizer.from_pretrained(model_id)
243 | self.model = AutoModel.from_pretrained(model_id, device_map='cpu')
244 | self.device = torch.device('cpu')
245 | self.model.to(self.device)
246 | self.model.eval()
247 |
248 | @torch.no_grad()
249 | def _encode_queries(self, queries):
250 | query_formatted = [self.QUERY_INST + queries] if isinstance(queries, str) else ['{}{}'.format(self.QUERY_INST, q) for q in queries]
251 | query_tokenized = self.tokenizer(query_formatted, padding=True, truncation=True, return_tensors='pt').to(self.device)
252 | last_hidden_states = self.model(**query_tokenized, return_dict=True).last_hidden_state
253 | embeddings = last_hidden_states[:, 0, :]
254 | embeddings = torch.nn.functional.normalize(embeddings, dim=-1)
255 | return embeddings.cpu().numpy()
256 |
257 | def retrieve(self, user_request, n_topk=3, src='huggingface', template='Modify the code below to solve this problem: {user_request}\n```python\n{retrieved_code}\n```'):
258 | config = self.configs[src]
259 | res = self.resources[src]
260 | index = res["index"]
261 | df = res["df"]
262 | q_embeddings = self._encode_queries([user_request])
263 | scores, indices = index.search(q_embeddings, n_topk*30)
264 | df_topk = df.iloc[indices[0]]
265 | process_func = config.get('process_data')
266 | if process_func:
267 | df_topk = process_func(df_topk)
268 | df_topk = df_topk.iloc[:n_topk]
269 | df_topk['user_request'] = user_request
270 | # return df_topk.reset_index(drop=True)
271 | ls_topk = df_topk.apply(lambda row: template.format(**row), axis=1).tolist()
272 | return ls_topk
273 |
274 | class LM:
275 | @torch.no_grad()
276 | def __init__(self, model_id = 'TheBloke/WizardCoder-Python-7B-V1.0-GPTQ', default_prohibitions=True):
277 | if '-GPTQ' in model_id:
278 | log('LM(gptq)')
279 | self.model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto").eval()
280 | self.model_device = self.model.device
281 | self.tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)
282 | # elif 'microsoft/phi-1' in model_id:
283 | # log('LM(phi)')
284 | # torch.set_default_device("cuda")
285 | # self.model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).eval()
286 | # self.model_device = self.model.device
287 | # self.tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
288 | # elif '-AWQ' in model_id:
289 | # log('LM(awq)')
290 | # from awq import AutoAWQForCausalLM
291 | # self.model = AutoAWQForCausalLM.from_quantized(model_id, fuse_layers=True, trust_remote_code=False, safetensors=True) # ?eval()
292 | # self.model_device = 'cuda'
293 | # self.tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=False)
294 | else:
295 | log('LM(hf)')
296 | self.model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, low_cpu_mem_usage=True, device_map="auto").eval()
297 | # self.model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).eval()
298 | self.model_device = self.model.device
299 | self.tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)
300 |
301 | self._init_tokenizer()
302 | self.default_prohibitions = []
303 | if default_prohibitions is True:
304 | self.default_prohibitions += self._subsentence_tokenizer(['\n\n\n', '\n\r\n\n', '\n\r\n\r'])
305 | elif default_prohibitions is False:
306 | pass
307 | else:
308 | self.default_prohibitions += self._subsentence_tokenizer(default_prohibitions)
309 | log(f'{self.default_prohibitions=}', 5)
310 | log(f'{self._lf=}', 5)
311 |
312 | def _init_tokenizer(self):
313 | if all(len(sublist) == 1 for sublist in self.tokenizer(['\n', '\n\n']).input_ids):
314 | self._lf = None
315 | else:
316 | self._lf = self.tokenizer('\n', add_special_tokens=False).input_ids
317 |
318 | def _subsentence_tokenizer(self, ls):
319 | if len(ls) < 1:
320 | return []
321 | ls = [ls] if isinstance(ls, str) else list(ls)
322 | if self._lf is None:
323 | return self.tokenizer(ls).input_ids
324 | ls = ['\n'+s for s in ls]
325 | ii = self.tokenizer(ls, add_special_tokens=False).input_ids
326 | ii = [i[len(self._lf):] for i in ii]
327 | return ii
328 |
329 | def _subsentence_decoder(self, ls):
330 | if self._lf is None:
331 | return self.tokenizer.decode(ls)
332 | return self.tokenizer.decode([self._lf[-1]] + list(ls))[1:]
333 |
334 | @torch.no_grad()
335 | def _constrained_beam(self, input_beam, constraint, prohibitions, num_beams, norm_factor = .0, patience_limit = 10):
336 | max_new_tokens, required_tokens = constraint
337 | required_tokens_pt = [torch.tensor(i).unsqueeze(0).to(self.model_device) for i in required_tokens]
338 | beams = [(input_beam[0], [], 0.0, input_beam[3])]
339 | best_postfixed = (torch.cat((input_beam[0], torch.tensor(required_tokens[0]).unsqueeze(0).to(self.model_device)), dim=1), required_tokens[0], input_beam[2], input_beam[3])
340 | patience = float('inf')
341 | for i in range(max_new_tokens):
342 | if patience < 0:
343 | break
344 | else:
345 | patience -= 1
346 | new_beams = []
347 | for beam in beams:
348 | beam_input_ids, beam_output_tokens, beam_score, beam_kv = beam
349 | new_outputs = self.model(beam_input_ids, use_cache=True, past_key_values=beam_kv)
350 | new_logits = new_outputs.logits[:, -1, :]
351 | new_kv = new_outputs.past_key_values
352 | topk = torch.topk(new_logits, num_beams)
353 | list_next_token_id = topk.indices[0]
354 | list_next_score = topk.values[0]
355 | for next_token_id, next_score in zip(list_next_token_id, list_next_score):
356 | new_input_ids = next_token_id.unsqueeze(0).unsqueeze(0)
357 | new_output_tokens = beam_output_tokens + [next_token_id.item()]
358 | new_score = ((beam_score * (len(beam_output_tokens) + norm_factor)) + next_score.item()) / (len(new_output_tokens) + norm_factor)
359 | if all(new_output_tokens[-len(p):] != p for p in prohibitions) and (next_token_id != self.tokenizer.eos_token_id):
360 | new_beam = (new_input_ids, new_output_tokens, new_score, new_kv)
361 | if any(new_beam[1][-len(sublist):] == sublist for sublist in required_tokens):
362 | if new_beam[2] > best_postfixed[2]:
363 | patience = patience_limit
364 | best_postfixed = new_beam
365 | else:
366 | new_beams.append(new_beam)
367 | new_beams = sorted(new_beams, key=lambda x: x[2], reverse=True)[:num_beams]
368 | beams = new_beams
369 | return best_postfixed
370 |
371 | @torch.no_grad()
372 | def _unconstrained_beam(self, input_beam, max_new_tokens, prohibitions, num_beams, norm_factor = .0, patience_limit = 10):
373 | beams = [(input_beam[0], [], 0.0, input_beam[3])]
374 | best_eos = (None, None, float('-inf'), None)
375 | patience = float('inf')
376 | for i in range(max_new_tokens):
377 | if patience < 0:
378 | break
379 | else:
380 | patience -= 1
381 | new_beams = []
382 | for beam in beams:
383 | beam_input_ids, beam_output_tokens, beam_score, beam_kv = beam
384 | new_outputs = self.model(beam_input_ids, use_cache=True, past_key_values=beam_kv)
385 | new_logits = new_outputs.logits[:, -1, :]
386 | new_kv = new_outputs.past_key_values
387 | topk = torch.topk(new_logits, num_beams)
388 | for next_token_id, next_score in zip(topk.indices[0], topk.values[0]):
389 | new_input_ids = next_token_id.unsqueeze(0).unsqueeze(0)
390 | new_output_tokens = beam_output_tokens + [next_token_id.item()]
391 | new_score = ((beam_score * (len(beam_output_tokens) + norm_factor)) + next_score.item()) / (len(new_output_tokens) + norm_factor)
392 | if (next_token_id == self.tokenizer.eos_token_id) and (new_score > best_eos[2]):
393 | best_eos = beam
394 | patience = patience_limit
395 | elif all(new_output_tokens[-len(p):] != p for p in prohibitions):
396 | new_beams.append((new_input_ids, new_output_tokens, new_score, new_kv))
397 | new_beams = sorted(new_beams, key=lambda x: x[2], reverse=True)[:num_beams]
398 | beams = new_beams
399 | result = best_eos if best_eos[1] else beams[0]
400 | return result
401 |
402 |
403 | def _get_constraints(self, template, default_padding=1, default_interval=500):
404 | if len(template) == 1:
405 | if isinstance(template[0], int):
406 | return [(template[0], [])]
407 | return [(default_padding, self._subsentence_tokenizer(template[0]))]
408 | template = list(template)
409 | template = [default_padding] + template if not isinstance(template[0], int) else template
410 | template = template + [''] if isinstance(template[-1], int) else template
411 | fixed_template = []
412 | expect_int = True
413 | for i in template:
414 | if (expect_int is True):
415 | if (isinstance(i, int)):
416 | fixed_template.append(i)
417 | expect_int = False
418 | else:
419 | fixed_template.extend([default_interval, i])
420 | expect_int = True
421 | else:
422 | fixed_template.append(i)
423 | expect_int = True
424 | assert len(fixed_template) % 2 == 0
425 | constraints = [(fixed_template[i], self._subsentence_tokenizer(fixed_template[i+1])) for i in range(0, len(fixed_template), 2)]
426 | return constraints
427 |
428 | def _get_prohibitions(self, ls):
429 | if ls is None:
430 | return self.default_prohibitions
431 | ls = ls + [' '+i for i in ls if not i[0].isspace()]
432 | return self.default_prohibitions + self._subsentence_tokenizer(ls)
433 |
434 | @torch.no_grad()
435 | def _generate(self, input_txt, constraints, prohibitions, num_beams):
436 | log(f'{constraints=}\n{prohibitions=}')
437 | input_ids = self.tokenizer(input_txt, add_special_tokens=True, return_tensors='pt').input_ids
438 | beam = (input_ids.to(self.model_device), [], .0, None)
439 | result = []
440 | for constraint in constraints:
441 | if len(constraint[1]) < 1:
442 | beam = self._unconstrained_beam(beam, max_new_tokens = constraint[0], prohibitions=prohibitions, num_beams=num_beams)
443 | to_decode = beam[1]
444 | result.append(to_decode)
445 | else:
446 | beam = self._constrained_beam(beam, constraint = constraint, prohibitions=prohibitions, num_beams=num_beams)
447 | to_decode = beam[1]
448 | result.extend([[to_decode[:-len(p)], p] for p in constraint[1] if to_decode[-len(p):] == p][0])
449 | # print(self._subsentence_decoder(to_decode), '\n---------\n')# debug
450 | result = [self._subsentence_decoder(i) for i in result]
451 | return result
452 |
453 | @torch.no_grad()
454 | def generate(self, input_txt, template = (('\n```python', '\n```sh'), '\n```'), constraints = None, prohibitions = None, num_beams = 3, join = True):
455 | constraints = self._get_constraints(template) if constraints is None else constraints
456 | prohibitions = self._get_prohibitions(prohibitions)
457 | result = self._generate(input_txt, constraints, prohibitions, num_beams)
458 | torch.cuda.empty_cache()
459 | if join is True:
460 | return ''.join(result)
461 | return result
462 |
463 | class Roy:
464 | def __init__(self, config=None):
465 | if config is None:
466 | config = {}
467 | self.template = config.get('template', "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:")
468 | self._venv = config.get('venv', None)
469 | self._lm = config.get('lm', None)
470 | self._rm = config.get('rm', None)
471 |
472 | def format(self, instruction, data=None):
473 | if data is None:
474 | data={}
475 | template = self.template.format(instruction=instruction.rstrip())
476 | if len(data) < 1:
477 | return template
478 | elif isinstance(data, pd.DataFrame):
479 | return data.apply(lambda row: template.format(**row), axis=1).tolist()
480 | elif isinstance(data, (dict, pd.Series)):
481 | return template.format(**data)
482 | else:
483 | raise ValueError("Unsupported data type. Data must be a dict, Series, or DataFrame.")
484 |
485 | def add_tool(self, fxn, key = None):
486 | key = fxn.__name__ if key is None else key
487 | setattr(self, key, types.MethodType(fxn, self))
488 |
489 | @property
490 | def venv(self):
491 | if self._venv is None:
492 | self._venv = VirtualEnvironment()
493 | return self._venv
494 |
495 | @property
496 | def lm(self):
497 | if self._lm is None:
498 | self._lm = LM()
499 | return self._lm
500 |
501 | @property
502 | def rm(self):
503 | if self._rm is None:
504 | self._rm = RM()
505 | return self._rm
506 |
507 | @trace_method
508 | def execute(self, *args, **kwargs):
509 | return self.venv.execute(*args, **kwargs)
510 |
511 | @trace_method
512 | def generate(self, *args, **kwargs):
513 | return self.lm.generate(*args, **kwargs)
514 |
515 | @trace_method
516 | def retrieve(self, *args, **kwargs):
517 | return self.rm.retrieve(*args, **kwargs)
518 |
519 | class Roys(Roy):
520 | def create(self, agents, tools=None):
521 | df_agents = pd.DataFrame(agents.items(), columns=['name', 'signature'])
522 | df_agents['chopchop'] = df_agents['signature'].apply(lambda x: [item.strip() for item in re.split(r'[=()]', x) if item.strip()])
523 | df_agents['in'] = df_agents['chopchop'].apply(lambda x: x[-1] if x else None)
524 | df_agents['to'] = df_agents['chopchop'].apply(lambda x: x[0] if x else None)
525 | df_agents['fxn'] = df_agents['chopchop'].apply(lambda x: x[1:-1] if x else None)
526 | df_agents = df_agents.drop(columns = ['chopchop'])
527 | self.df_agents = df_agents
528 | if tools is not None:
529 | for key, val in tools.items():
530 | if 'self' in inspect.signature(val).parameters:
531 | self.add_tool(val, key)
532 | else:
533 | setattr(self, key, trace_method(val))
534 |
535 | def _map_fxn(self, ls_fxn, ls_i):
536 | ls_i = [ls_i] if isinstance(ls_i, str) else ls_i
537 | ls_o = []
538 | for i in ls_i:
539 | t = i
540 | for f in ls_fxn[::-1]:
541 | t = getattr(self, f)(t)
542 | if isinstance(t, list):
543 | ls_o.extend(t)
544 | elif isinstance(t, str):
545 | ls_o.append(t)
546 | else:
547 | continue
548 | return ls_o
549 |
550 | def start(self, requests):
551 | self.dict_cache = {key: [value] if isinstance(value, str) else value for key, value in requests.items()}
552 | for turn in range(2):
553 | if '_' in self.dict_cache:
554 | break
555 | log(f'Turn {turn}', 1)
556 | snapshot = copy.deepcopy(self.dict_cache)
557 | for _, row_agent in self.df_agents.iterrows():
558 | key_i = row_agent['in']
559 | key_o = row_agent['to']
560 | ls_fxn = row_agent['fxn']
561 | if key_i in snapshot.keys():
562 | agent_output = self._map_fxn(ls_fxn, snapshot[key_i])
563 | self.dict_cache[key_o] = agent_output
564 | _log = '\n'.join(agent_output)
565 | log(f"<<<{row_agent['name']}>>>:\n{_log}", 0)
566 | return self.dict_cache
567 |
--------------------------------------------------------------------------------
/quickstart.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "colab_type": "text",
7 | "id": "view-in-github"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {
16 | "id": "_SPXb5uyf_Vd"
17 | },
18 | "source": [
19 | "# Roy"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": 2,
25 | "metadata": {
26 | "colab": {
27 | "base_uri": "https://localhost:8080/"
28 | },
29 | "id": "_hc8-O2PfsE8",
30 | "outputId": "873498ff-77b3-42b7-a507-5437d605c7af"
31 | },
32 | "outputs": [
33 | {
34 | "name": "stdout",
35 | "output_type": "stream",
36 | "text": [
37 | "/content/Roy\n"
38 | ]
39 | },
40 | {
41 | "name": "stderr",
42 | "output_type": "stream",
43 | "text": [
44 | "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
45 | ]
46 | }
47 | ],
48 | "source": [
49 | "!git clone https://github.com/JosefAlbers/Roy\n",
50 | "%cd Roy\n",
51 | "!pip install -qqq -r requirements.txt\n",
52 | "!pip install -qqqU transformers optimum accelerate auto-gptq --extra-index-url https://huggingface.github.io/autogptq-index/whl/cu118/\n",
53 | "\n",
54 | "from roy import Roy\n",
55 | "roy = Roy()"
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": 2,
61 | "metadata": {
62 | "colab": {
63 | "base_uri": "https://localhost:8080/"
64 | },
65 | "id": "xX95zSZ4fpLz",
66 | "outputId": "1412d319-1c28-4828-90cd-b5729e2cd59e"
67 | },
68 | "outputs": [
69 | {
70 | "name": "stdout",
71 | "output_type": "stream",
72 | "text": [
73 | "\u001b[35mformat() receives:\n",
74 | " Compare the year-to-date gain for META and TESLA.\u001b[0m\n",
75 | "\u001b[34mformat() returns:\n",
76 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
77 | "\n",
78 | " ### Instruction:\n",
79 | " Compare the year-to-date gain for META and TESLA.\n",
80 | "\n",
81 | " ### Response:\u001b[0m\n",
82 | "\u001b[35mgenerate() receives:\n",
83 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
84 | "\n",
85 | " ### Instruction:\n",
86 | " Compare the year-to-date gain for META and TESLA.\n",
87 | "\n",
88 | " ### Response:\u001b[0m\n",
89 | "\u001b[34mgenerate() returns:\n",
90 | " To compare the year-over-year gains for META and TESLA, we need to look at their financial statements for the current year and the previous year. Here is an overview of how we can do it:\n",
91 | "\n",
92 | " 1. First, we need access to the financial statements of META and TESLA.\n",
93 | " 2. Next, we need to find the year-over-year revenue and earnings for each of them.\n",
94 | " 3. Then, we need to calculate the year-over-year change in revenue and earnings.\n",
95 | " 4. Finally, we need to compare the year-over-year gains for META and TESLA.\n",
96 | "\n",
97 | " Let's assume that META and TESLA's revenue and earnings for the current and previous years are as follows:\n",
98 | "\n",
99 | " META:\n",
100 | "\n",
101 | " Revenue for the current year: $500 million\n",
102 | " Revenue for the previous year: $400 million\n",
103 | "\n",
104 | " Earnings for the current year: $30 million\n",
105 | " Earnings for the previous year: $20 million\n",
106 | "\n",
107 | " TESLA:\n",
108 | "\n",
109 | " Revenue for the current year: $600 million\n",
110 | " Revenue for the previous year: $500 million\n",
111 | "\n",
112 | " Earnings for the current year: $40 million\n",
113 | " Earnings for the previous year: $30 million\n",
114 | "\n",
115 | " To calculate year-over-year revenue gains for META:\n",
116 | "\n",
117 | " Year-over-year revenue gain = (Revenue for the current year - Revenue for the previous year) / Revenue for the previous year\n",
118 | "\n",
119 | " Year-over-year revenue gain for META = ((500 - 400) / 400)\n",
120 | " Year-over-year revenue gain for META = 25%\n",
121 | "\n",
122 | " To calculate year-over-year revenue gains for TESLA:\n",
123 | "\n",
124 | " Year-over-year revenue gain = (Revenue for the current year - Revenue for the previous year) / Revenue for the previous year\n",
125 | "\n",
126 | " Year-over-year revenue gain for TESLA = ((600 - 500\n",
127 | " ```\n",
128 | " Year-over-year revenue gain for TESLA = ((600 - 500) / 500)\n",
129 | " Year-over-year revenue gain for TESLA = 50%\n",
130 | " ```\n",
131 | "\n",
132 | " To calculate year-over-year earnings gains for META:\n",
133 | "\n",
134 | " Year-over-year earnings gain = (Earnings for the current year - Earnings for the previous year) / Earnings for the previous year\n",
135 | "\n",
136 | " Year-over-year earnings gain for META = ((30 - 20) / 20)\n",
137 | " Year-over-year earnings gain for META = 50%\n",
138 | "\n",
139 | " To calculate year-over-year earnings gains for TESLA:\n",
140 | "\n",
141 | " Year-over-year earnings gain = (Earnings for the current year - Earnings for the previous year) / Earnings for the previous year\n",
142 | "\n",
143 | " Year-over-year earnings gain for TESLA = ((40 - 30) / 30)\n",
144 | " Year-over-year earnings gain for TESLA = 25%\n",
145 | "\u001b[0m\n"
146 | ]
147 | }
148 | ],
149 | "source": [
150 | "s = 'What date is today? Which big tech stock has the largest year-to-date gain this year? How much is the gain?'\n",
151 | "roy.generate(roy.format(s))\n",
152 | "\n",
153 | "roy.generate(s, ('\\n```python', '\\n```')) # Generate a python code block\n",
154 | "roy.generate(s, (('\\n```python', '\\n```javascript'), '\\n```')) # Generate python or javascript codes\n",
155 | "roy.generate(s, ('\\n```python', 100, '\\n```')) # Generate a code block of size less than 100 tokens"
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": 3,
161 | "metadata": {
162 | "colab": {
163 | "base_uri": "https://localhost:8080/"
164 | },
165 | "id": "mUzVzngafqip",
166 | "outputId": "d8ad31ad-bac8-46e0-d86d-f3695edced3c"
167 | },
168 | "outputs": [
169 | {
170 | "name": "stdout",
171 | "output_type": "stream",
172 | "text": [
173 | "\u001b[35mretrieve() receives:\n",
174 | " Create a text to image generator.\u001b[0m\n",
175 | "\u001b[34mretrieve() returns an object of type: \u001b[0m\n",
176 | "\u001b[35mformat() receives:\n",
177 | " Modify the [Example Code] to fulfill the [User Request] using minimal changes. Keep the modifications minimal by making only the necessary modifications.\n",
178 | "\n",
179 | " [User Request]:\n",
180 | " \"{user_request}\"\n",
181 | "\n",
182 | " [Context]:\n",
183 | " {retrieved_docstr}\n",
184 | "\n",
185 | " [Example Code]:\n",
186 | " ```python\n",
187 | " {retrieved_code}\n",
188 | " ```\u001b[0m\n",
189 | "\u001b[34mformat() returns an object of type: \u001b[0m\n",
190 | "\u001b[35mgenerate() receives:\n",
191 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
192 | "\n",
193 | " ### Instruction:\n",
194 | " Modify the [Example Code] to fulfill the [User Request] using minimal changes. Keep the modifications minimal by making only the necessary modifications.\n",
195 | "\n",
196 | " [User Request]:\n",
197 | " \"Create a text to image generator.\"\n",
198 | "\n",
199 | " [Context]:\n",
200 | " class `ImageToTextPipeline` (transformers/src/transformers/pipelines/image_to_text.py/ImageToTextPipeline):\n",
201 | " '''\n",
202 | " Image To Text pipeline using a `AutoModelForVision2Seq`. This pipeline predicts a caption for a given image.\n",
203 | "\n",
204 | " Example:\n",
205 | "\n",
206 | " ```python\n",
207 | " >>> from transformers import pipeline\n",
208 | "\n",
209 | " >>> captioner = pipeline(model=\"ydshieh/vit-gpt2-coco-en\")\n",
210 | " >>> captioner(\"https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png\")\n",
211 | " [{'generated_text': 'two birds are standing next to each other '}]\n",
212 | " ```\n",
213 | "\n",
214 | " Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)\n",
215 | "\n",
216 | " This image to text pipeline can currently be loaded from pipeline() using the following task identifier:\n",
217 | " \"image-to-text\".\n",
218 | "\n",
219 | " See the list of available models on\n",
220 | " [huggingface.co/models](https://huggingface.co/models?pipeline_tag=image-to-text)....\n",
221 | " '''\n",
222 | "\n",
223 | " [Example Code]:\n",
224 | " ```python\n",
225 | " >>> from transformers import pipeline\n",
226 | "\n",
227 | " >>> captioner = pipeline(model=\"ydshieh/vit-gpt2-coco-en\")\n",
228 | " >>> captioner(\"https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png\")\n",
229 | " [{'generated_text': 'two birds are standing next to each other '}]\n",
230 | "\n",
231 | " ```\n",
232 | "\n",
233 | " ### Response:\u001b[0m\n",
234 | "\u001b[34mgenerate() returns:\n",
235 | " Here's the modified `ImageToTextPipeline`:\n",
236 | " ```python\n",
237 | " import os\n",
238 | "\n",
239 | " import torch\n",
240 | "\n",
241 | " from PIL import Image\n",
242 | "\n",
243 | " import numpy as np\n",
244 | "\n",
245 | " import requests\n",
246 | "\n",
247 | " from transformers import pipeline\n",
248 | " from transformers.file_utils import cached_path\n",
249 | "\n",
250 | " from transformers.pipelines.base import PIPELINES\n",
251 | "\n",
252 | " from transformers.pipelines.image_classification import (\n",
253 | " ImageClassificationPipeline,\n",
254 | " )\n",
255 | "\n",
256 | " from transformers.pipelines.image_prediction import (\n",
257 | " ObjectDetectionPipeline,\n",
258 | " )\n",
259 | "\n",
260 | " @PIPELINES.register()\n",
261 | "\n",
262 | " class ImageToTextPipeline(ImageClassificationPipeline, ObjectDetectionPipeline):\n",
263 | "\n",
264 | " def __init__(\n",
265 | " self,\n",
266 | " model,\n",
267 | " tokenizer=None,\n",
268 | "\n",
269 | " ):\n",
270 | "\n",
271 | " super().__init__(\n",
272 | " model,\n",
273 | " tokenizer,\n",
274 | "\n",
275 | " )\n",
276 | "\n",
277 | " \n",
278 | "\n",
279 | " @staticmethod\n",
280 | "\n",
281 | " def load_image(image):\n",
282 | "\n",
283 | " if isinstance(image, str):\n",
284 | "\n",
285 | " if \"http\" in image:\n",
286 | "\n",
287 | " response = requests.get(image)\n",
288 | "\n",
289 | " content = response.content\n",
290 | "\n",
291 | " else:\n",
292 | "\n",
293 | " content = image\n",
294 | " \n",
295 | " else:\n",
296 | "\n",
297 | " content = image\n",
298 | " \n",
299 | " \n",
300 | "\n",
301 | " return content\n",
302 | " \n",
303 | " \n",
304 | " @staticmethod\n",
305 | "\n",
306 | " def resize_and_center_crop(image):\n",
307 | "\n",
308 | " width, height = image.size\n",
309 | "\n",
310 | " \n",
311 | "\n",
312 | " new_width, new_height = 224, 224\n",
313 | " \n",
314 | " \n",
315 | "\n",
316 | " left = (width - new_width) // 2\n",
317 | " \n",
318 | "\n",
319 | " right = width - left\n",
320 | " \n",
321 | " \n",
322 | " upper = (height - new_height) // 2\n",
323 | " \n",
324 | " lower = height - upper\n",
325 | " \n",
326 | " \n",
327 | "\n",
328 | " return image.crop((left, upper, right, lower)\n",
329 | "\n",
330 | " \n",
331 | " def __call__(self, images, *args, **kwargs):\n",
332 | " \n",
333 | " if isinstance(images, str):\n",
334 | " \n",
335 | " images = [images]\n",
336 | " \n",
337 | " \n",
338 | " outputs = []\n",
339 | " \n",
340 | " for image in images:\n",
341 | " \n",
342 | " if isinstance(image, str):\n",
343 | " \n",
344 | " image = self.load_image(image)\n",
345 | " \n",
346 | " \n",
347 | " image = self.resize_and_center_crop(image)\n",
348 | " \n",
349 | " inputs = self.tokenizer(\n",
350 | " images,\n",
351 | " return_tensors=\"pt\",\n",
352 | " padding=True,\n",
353 | " truncation=True,\n",
354 | " max_length=128,\n",
355 | " )\n",
356 | " \n",
357 | " outputs.append(self.model.generate(**inputs)[0])\n",
358 | " \n",
359 | " return outputs\n",
360 | " ```\u001b[0m\n",
361 | "\u001b[35mgenerate() receives:\n",
362 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
363 | "\n",
364 | " ### Instruction:\n",
365 | " Modify the [Example Code] to fulfill the [User Request] using minimal changes. Keep the modifications minimal by making only the necessary modifications.\n",
366 | "\n",
367 | " [User Request]:\n",
368 | " \"Create a text to image generator.\"\n",
369 | "\n",
370 | " [Context]:\n",
371 | " function `InferenceClient.text_to_image` (huggingface_hub/src/huggingface_hub/inference/_client.py/InferenceClient.text_to_image):\n",
372 | " '''\n",
373 | " Generate an image based on a given text using a specified model.\n",
374 | "\n",
375 | " \n",
376 | "\n",
377 | " You must have `PIL` installed if you want to work with images (`pip install Pillow`).\n",
378 | "\n",
379 | " \n",
380 | "\n",
381 | " Args:\n",
382 | " prompt (`str`):\n",
383 | " The prompt to generate an image from.\n",
384 | " negative_prompt (`str`, *optional*):\n",
385 | " An optional negative prompt for the image generation.\n",
386 | " height (`float`, *optional*):\n",
387 | " The height in pixels of the image to generate.\n",
388 | " width (`float`, *optional*):\n",
389 | " The width in pixels of the image to generate.\n",
390 | " num_inference_steps (`int`, *optional*):\n",
391 | " The number of denoising steps. More denoising steps usually lead to a higher quality image at the\n",
392 | " expense of slower inference.\n",
393 | " guidance_scale (`float`, *optional*):\n",
394 | " Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,\n",
395 | " usually at the expense of lower image quality.\n",
396 | " model (`str`, *optional*):\n",
397 | " The model to use for inference. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed\n",
398 | " Inference Endpoint. This parameter overrides the model defined at the instance level. Defaults to None.\n",
399 | "\n",
400 | " Returns:\n",
401 | " `Image`: The generated image.\n",
402 | "\n",
403 | " Raises:\n",
404 | " [`InferenceTimeoutError`]:\n",
405 | " If the model is unavailable or the request times out.\n",
406 | " `HTTPError`:\n",
407 | " If the request fails with an HTTP error status code other than HTTP 503.\n",
408 | "\n",
409 | " Example:\n",
410 | " ```py\n",
411 | " >>> from huggingface_hub import InferenceClient\n",
412 | " >>> client = InferenceClient()\n",
413 | "\n",
414 | " >>> image = client.text_to_image(\"An astronaut riding a horse on the moon.\")\n",
415 | " >>> image.save(\"astronaut.png\")\n",
416 | "\n",
417 | " >>> image = client.text_to_image(\n",
418 | " ... \"An astronaut riding a horse on the moon.\",\n",
419 | " ... negative_prompt=\"low resolution, blurry\",\n",
420 | " ... model=\"stabilityai/stable-diffusion-2-1\",\n",
421 | " ... )\n",
422 | " >>> image.save(\"better_astronaut.png\")\n",
423 | " ```...\n",
424 | " '''\n",
425 | "\n",
426 | " [Example Code]:\n",
427 | " ```python\n",
428 | " >>> from huggingface_hub import InferenceClient\n",
429 | " >>> client = InferenceClient()\n",
430 | "\n",
431 | " >>> image = client.text_to_image(\"An astronaut riding a horse on the moon.\")\n",
432 | " >>> image.save(\"astronaut.png\")\n",
433 | "\n",
434 | " >>> image = client.text_to_image(\n",
435 | " ... \"An astronaut riding a horse on the moon.\",\n",
436 | " ... negative_prompt=\"low resolution, blurry\",\n",
437 | " ... model=\"stabilityai/stable-diffusion-2-1\",\n",
438 | " ... )\n",
439 | " >>> image.save(\"better_astronaut.png\")\n",
440 | "\n",
441 | " ```\n",
442 | "\n",
443 | " ### Response:\u001b[0m\n",
444 | "\u001b[34mgenerate() returns:\n",
445 | " Here's the modified `text_to_image` function that fulfills the [User Request]:\n",
446 | " ```python\n",
447 | " import requests\n",
448 | " from io import BytesIO\n",
449 | " from PIL import Image\n",
450 | "\n",
451 | " def text_to_image(prompt: str,\n",
452 | " negative_prompt: str = None,\n",
453 | " height: int = None,\n",
454 | " width: int = None,\n",
455 | " num_inference_steps: int = None,\n",
456 | " guidance_scale: float = None,\n",
457 | " model: str = None):\n",
458 | " \n",
459 | " if model is None:\n",
460 | " raise ValueError(\n",
461 | " \"You need to specify the model to use for inference. \"\n",
462 | " \"You can provide a model ID hosted on the Hugging Face Hub \"\n",
463 | " \"or a URL to a deployed inference endpoint.\"\n",
464 | " )\n",
465 | " \n",
466 | "\n",
467 | " payload = {\n",
468 | " \"prompt\": prompt,\n",
469 | " }\n",
470 | " \n",
471 | "\n",
472 | " if negative_prompt is not None:\n",
473 | " payload[\"negative_prompt\"] = negative_prompt\n",
474 | " \n",
475 | "\n",
476 | " if height is not None:\n",
477 | " payload[\"height\"] = height\n",
478 | " \n",
479 | "\n",
480 | " if width is not None:\n",
481 | " payload[\"width\"] = width\n",
482 | " \n",
483 | "\n",
484 | " if num_inference_steps is not None:\n",
485 | " payload[\"steps\"] = num_inference_steps\n",
486 | " \n",
487 | "\n",
488 | " if guidance_scale is not None:\n",
489 | " payload[\"scale\"] = guidance_scale\n",
490 | " \n",
491 | "\n",
492 | " response = requests.post(model, json=payload)\n",
493 | " \n",
494 | "\n",
495 | " try:\n",
496 | " response.raise_for_status()\n",
497 | " \n",
498 | " except requests.exceptions.HTTPError:\n",
499 | " if response.status_code == 503:\n",
500 | "\n",
501 | " raise ValueError(\n",
502 | " f\"{response.json().get('detail')}\"\n",
503 | " )\n",
504 | " \n",
505 | " else:\n",
506 | "\n",
507 | " response.raise_for_status()\n",
508 | " \n",
509 | "\n",
510 | " image_bytes = BytesIO(response.content)\n",
511 | " \n",
512 | "\n",
513 | " try:\n",
514 | "\n",
515 | " image= Image.open(image_bytes)\n",
516 | " \n",
517 | " except IOError:\n",
518 | "\n",
519 | " raise ValueError(\"Failed to generate an image.\")\n",
520 | " \n",
521 | " \n",
522 | " return image\n",
523 | " ```\u001b[0m\n",
524 | "\u001b[35mgenerate() receives:\n",
525 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
526 | "\n",
527 | " ### Instruction:\n",
528 | " Modify the [Example Code] to fulfill the [User Request] using minimal changes. Keep the modifications minimal by making only the necessary modifications.\n",
529 | "\n",
530 | " [User Request]:\n",
531 | " \"Create a text to image generator.\"\n",
532 | "\n",
533 | " [Context]:\n",
534 | " function `LDMTextToImagePipeline.__call__` (diffusers/src/diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py/LDMTextToImagePipeline.__call__):\n",
535 | " '''\n",
536 | " The call function to the pipeline for generation.\n",
537 | "\n",
538 | " Args:\n",
539 | " prompt (`str` or `List[str]`):\n",
540 | " The prompt or prompts to guide the image generation.\n",
541 | " height (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):\n",
542 | " The height in pixels of the generated image.\n",
543 | " width (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):\n",
544 | " The width in pixels of the generated image.\n",
545 | " num_inference_steps (`int`, *optional*, defaults to 50):\n",
546 | " The number of denoising steps. More denoising steps usually lead to a higher quality image at the\n",
547 | " expense of slower inference.\n",
548 | " guidance_scale (`float`, *optional*, defaults to 1.0):\n",
549 | " A higher guidance scale value encourages the model to generate images closely linked to the text\n",
550 | " `prompt` at the expense of lower image quality. Guidance scale is enabled when `guidance_scale > 1`.\n",
551 | " generator (`torch.Generator`, *optional*):\n",
552 | " A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make\n",
553 | " generation deterministic.\n",
554 | " latents (`torch.FloatTensor`, *optional*):\n",
555 | " Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image\n",
556 | " generation. Can be used to tweak the same generation with different prompts. If not provided, a latents\n",
557 | " tensor is generated by sampling using the supplied random `generator`.\n",
558 | " output_type (`str`, *optional*, defaults to `\"pil\"`):\n",
559 | " The output format of the generated image. Choose between `PIL.Image` or `np.array`.\n",
560 | " return_dict (`bool`, *optional*, defaults to `True`):\n",
561 | " Whether or not to return a [`ImagePipelineOutput`] instead of a plain tuple.\n",
562 | "\n",
563 | " Example:\n",
564 | "\n",
565 | " ```py\n",
566 | " >>> from diffusers import DiffusionPipeline\n",
567 | "\n",
568 | " >>> # load model and scheduler\n",
569 | " >>> ldm = DiffusionPipeline.from_pretrained(\"CompVis/ldm-text2im-large-256\")\n",
570 | "\n",
571 | " >>> # run pipeline in inference (sample random noise and denoise)\n",
572 | " >>> prompt = \"A painting of a squirrel eating a burger\"\n",
573 | " >>> images = ldm([prompt], num_inference_steps=50, eta=0.3, guidance_scale=6).images\n",
574 | "\n",
575 | " >>> # save images\n",
576 | " >>> for idx, image in enumerate(images):\n",
577 | " ... image.save(f\"squirrel-{idx}.png\")\n",
578 | " ```\n",
579 | "\n",
580 | " Returns:\n",
581 | " [`~pipelines.ImagePipelineOutput`] or `tuple`:\n",
582 | " If `return_dict` is `True`, [`~pipelines.ImagePipelineOutput`] is returned, otherwise a `tuple` is\n",
583 | " returned where the first element is a list with the generated images....\n",
584 | " '''\n",
585 | "\n",
586 | " [Example Code]:\n",
587 | " ```python\n",
588 | " >>> from diffusers import DiffusionPipeline\n",
589 | "\n",
590 | " >>> # load model and scheduler\n",
591 | " >>> ldm = DiffusionPipeline.from_pretrained(\"CompVis/ldm-text2im-large-256\")\n",
592 | "\n",
593 | " >>> # run pipeline in inference (sample random noise and denoise)\n",
594 | " >>> prompt = \"A painting of a squirrel eating a burger\"\n",
595 | " >>> images = ldm([prompt], num_inference_steps=50, eta=0.3, guidance_scale=6).images\n",
596 | "\n",
597 | " >>> # save images\n",
598 | " >>> for idx, image in enumerate(images):\n",
599 | " ... image.save(f\"squirrel-{idx}.png\")\n",
600 | "\n",
601 | " ```\n",
602 | "\n",
603 | " ### Response:\u001b[0m\n",
604 | "\u001b[34mgenerate() returns:\n",
605 | " Here's the modified `LDMTextToImagePipeline.__call__` function that fulfills the [User Request]:\n",
606 | "\n",
607 | " ```python\n",
608 | " def __call__(\n",
609 | " self,\n",
610 | " prompt,\n",
611 | " height: Optional[int] = None,\n",
612 | " width: Optional[int] = None,\n",
613 | " num_inference_steps: Optional[int] = None,\n",
614 | " guidance_scale: Optional[float] = None,\n",
615 | " ):\n",
616 | "\n",
617 | " if height is None:\n",
618 | " height = self.unet.config.sample_size\n",
619 | "\n",
620 | " if width is None:\n",
621 | " width = height\n",
622 | "\n",
623 | " if num_inference_steps is None:\n",
624 | " num_inference_steps = 50\n",
625 | "\n",
626 | " if guidance_scale is None:\n",
627 | " guidance_scale = 1.0\n",
628 | " \n",
629 | "\n",
630 | " prompts = prompt if isinstance(prompt, list) else [prompt]\n",
631 | "\n",
632 | " if guidance_scale > 1:\n",
633 | " prompts = self._add_guidance(prompts)\n",
634 | "\n",
635 | " if len(prompts) == 1:\n",
636 | "\n",
637 | " prompts[0]\n",
638 | " \n",
639 | "\n",
640 | " latents = self._generate_noise()\n",
641 | "\n",
642 | " images = []\n",
643 | "\n",
644 | " for prompt in prompts:\n",
645 | " \n",
646 | "\n",
647 | " for step in range(num_inference_steps):\n",
648 | " \n",
649 | "\n",
650 | " latents = self._denoise(\n",
651 | " latents,\n",
652 | " )\n",
653 | "\n",
654 | " images.append(\n",
655 | " self._decode(latents)\n",
656 | " )\n",
657 | " \n",
658 | "\n",
659 | " return images\n",
660 | " ``` \u001b[0m\n"
661 | ]
662 | },
663 | {
664 | "data": {
665 | "text/plain": [
666 | "['Here\\'s the modified `ImageToTextPipeline`:\\n```python\\nimport os\\n\\nimport torch\\n\\nfrom PIL import Image\\n\\nimport numpy as np\\n\\nimport requests\\n\\nfrom transformers import pipeline\\nfrom transformers.file_utils import cached_path\\n\\nfrom transformers.pipelines.base import PIPELINES\\n\\nfrom transformers.pipelines.image_classification import (\\n ImageClassificationPipeline,\\n)\\n\\nfrom transformers.pipelines.image_prediction import (\\n ObjectDetectionPipeline,\\n)\\n\\n@PIPELINES.register()\\n\\nclass ImageToTextPipeline(ImageClassificationPipeline, ObjectDetectionPipeline):\\n\\n def __init__(\\n self,\\n model,\\n tokenizer=None,\\n\\n ):\\n\\n super().__init__(\\n model,\\n tokenizer,\\n\\n )\\n\\n \\n\\n @staticmethod\\n\\n def load_image(image):\\n\\n if isinstance(image, str):\\n\\n if \"http\" in image:\\n\\n response = requests.get(image)\\n\\n content = response.content\\n\\n else:\\n\\n content = image\\n \\n else:\\n\\n content = image\\n \\n \\n\\n return content\\n \\n \\n @staticmethod\\n\\n def resize_and_center_crop(image):\\n\\n width, height = image.size\\n\\n \\n\\n new_width, new_height = 224, 224\\n \\n \\n\\n left = (width - new_width) // 2\\n \\n\\n right = width - left\\n \\n \\n upper = (height - new_height) // 2\\n \\n lower = height - upper\\n \\n \\n\\n return image.crop((left, upper, right, lower)\\n\\n \\n def __call__(self, images, *args, **kwargs):\\n \\n if isinstance(images, str):\\n \\n images = [images]\\n \\n \\n outputs = []\\n \\n for image in images:\\n \\n if isinstance(image, str):\\n \\n image = self.load_image(image)\\n \\n \\n image = self.resize_and_center_crop(image)\\n \\n inputs = self.tokenizer(\\n images,\\n return_tensors=\"pt\",\\n padding=True,\\n truncation=True,\\n max_length=128,\\n )\\n \\n outputs.append(self.model.generate(**inputs)[0])\\n \\n return outputs\\n```',\n",
667 | " 'Here\\'s the modified `text_to_image` function that fulfills the [User Request]:\\n```python\\nimport requests\\nfrom io import BytesIO\\nfrom PIL import Image\\n\\ndef text_to_image(prompt: str,\\n negative_prompt: str = None,\\n height: int = None,\\n width: int = None,\\n num_inference_steps: int = None,\\n guidance_scale: float = None,\\n model: str = None):\\n \\n if model is None:\\n raise ValueError(\\n \"You need to specify the model to use for inference. \"\\n \"You can provide a model ID hosted on the Hugging Face Hub \"\\n \"or a URL to a deployed inference endpoint.\"\\n )\\n \\n\\n payload = {\\n \"prompt\": prompt,\\n }\\n \\n\\n if negative_prompt is not None:\\n payload[\"negative_prompt\"] = negative_prompt\\n \\n\\n if height is not None:\\n payload[\"height\"] = height\\n \\n\\n if width is not None:\\n payload[\"width\"] = width\\n \\n\\n if num_inference_steps is not None:\\n payload[\"steps\"] = num_inference_steps\\n \\n\\n if guidance_scale is not None:\\n payload[\"scale\"] = guidance_scale\\n \\n\\n response = requests.post(model, json=payload)\\n \\n\\n try:\\n response.raise_for_status()\\n \\n except requests.exceptions.HTTPError:\\n if response.status_code == 503:\\n\\n raise ValueError(\\n f\"{response.json().get(\\'detail\\')}\"\\n )\\n \\n else:\\n\\n response.raise_for_status()\\n \\n\\n image_bytes = BytesIO(response.content)\\n \\n\\n try:\\n\\n image= Image.open(image_bytes)\\n \\n except IOError:\\n\\n raise ValueError(\"Failed to generate an image.\")\\n \\n \\n return image\\n```',\n",
668 | " \"Here's the modified `LDMTextToImagePipeline.__call__` function that fulfills the [User Request]:\\n\\n```python\\ndef __call__(\\n self,\\n prompt,\\n height: Optional[int] = None,\\n width: Optional[int] = None,\\n num_inference_steps: Optional[int] = None,\\n guidance_scale: Optional[float] = None,\\n):\\n\\n if height is None:\\n height = self.unet.config.sample_size\\n\\n if width is None:\\n width = height\\n\\n if num_inference_steps is None:\\n num_inference_steps = 50\\n\\n if guidance_scale is None:\\n guidance_scale = 1.0\\n \\n\\n prompts = prompt if isinstance(prompt, list) else [prompt]\\n\\n if guidance_scale > 1:\\n prompts = self._add_guidance(prompts)\\n\\n if len(prompts) == 1:\\n\\n prompts[0]\\n \\n\\n latents = self._generate_noise()\\n\\n images = []\\n\\n for prompt in prompts:\\n \\n\\n for step in range(num_inference_steps):\\n \\n\\n latents = self._denoise(\\n latents,\\n )\\n\\n images.append(\\n self._decode(latents)\\n )\\n \\n\\n return images\\n``` \"]"
669 | ]
670 | },
671 | "execution_count": 3,
672 | "metadata": {},
673 | "output_type": "execute_result"
674 | }
675 | ],
676 | "source": [
677 | "s = 'Create a text to image generator.'\n",
678 | "r = roy.retrieve(s, n_topk=3, src='huggingface')\n",
679 | "[roy.generate(s) for s in r]"
680 | ]
681 | },
682 | {
683 | "cell_type": "code",
684 | "execution_count": 3,
685 | "metadata": {
686 | "colab": {
687 | "base_uri": "https://localhost:8080/"
688 | },
689 | "id": "qAxZPtgPfqzr",
690 | "outputId": "b3c0b025-9877-4905-8a3e-d2769b06ecf1"
691 | },
692 | "outputs": [
693 | {
694 | "name": "stdout",
695 | "output_type": "stream",
696 | "text": [
697 | "\u001b[35mgenerate() receives:\n",
698 | " Create a secure and unique secret code word with a Python script that involves multiple steps to ensure the highest level of confidentiality and protection.\u001b[0m\n",
699 | "\u001b[34mgenerate() returns:\n",
700 | "\n",
701 | " ```python\n",
702 | " import random\n",
703 | " import string\n",
704 | "\n",
705 | " def generate_secret_code():\n",
706 | "\n",
707 | " letters = string.ascii_letters\n",
708 | "\n",
709 | " secret_code = ''\n",
710 | " \n",
711 | " for i in range(5):\n",
712 | " \n",
713 | " secret_code += random.choice(letters)\n",
714 | " \n",
715 | " \n",
716 | " \n",
717 | " \n",
718 | " digits = string.digits\n",
719 | " \n",
720 | " \n",
721 | " for i in range(2):\n",
722 | " \n",
723 | " secret_code += random.choice(digits)\n",
724 | " \n",
725 | " \n",
726 | " \n",
727 | " \n",
728 | " special_chars = string.punctuation\n",
729 | " \n",
730 | " \n",
731 | " for i in range(1):\n",
732 | " \n",
733 | " secret_code += random.choice(special_chars)\n",
734 | " \n",
735 | " \n",
736 | " \n",
737 | " \n",
738 | " \n",
739 | " \n",
740 | " print(secret_code)\n",
741 | "\n",
742 | " generate_secret_code()\n",
743 | " ``` \u001b[0m\n",
744 | "\u001b[35mexecute() receives:\n",
745 | "\n",
746 | " ```python\n",
747 | " import random\n",
748 | " import string\n",
749 | "\n",
750 | " def generate_secret_code():\n",
751 | "\n",
752 | " letters = string.ascii_letters\n",
753 | "\n",
754 | " secret_code = ''\n",
755 | " \n",
756 | " for i in range(5):\n",
757 | " \n",
758 | " secret_code += random.choice(letters)\n",
759 | " \n",
760 | " \n",
761 | " \n",
762 | " \n",
763 | " digits = string.digits\n",
764 | " \n",
765 | " \n",
766 | " for i in range(2):\n",
767 | " \n",
768 | " secret_code += random.choice(digits)\n",
769 | " \n",
770 | " \n",
771 | " \n",
772 | " \n",
773 | " special_chars = string.punctuation\n",
774 | " \n",
775 | " \n",
776 | " for i in range(1):\n",
777 | " \n",
778 | " secret_code += random.choice(special_chars)\n",
779 | " \n",
780 | " \n",
781 | " \n",
782 | " \n",
783 | " \n",
784 | " \n",
785 | " print(secret_code)\n",
786 | "\n",
787 | " generate_secret_code()\n",
788 | " ``` \u001b[0m\n",
789 | "\u001b[34mexecute() returns:\n",
790 | "\n",
791 | " Traceback (most recent call last):\n",
792 | " File \"/content/Roy/script.py\", line 39, in \n",
793 | " print(secret_code)\n",
794 | " NameError: name 'secret_code' is not defined\n",
795 | "\u001b[0m\n",
796 | "\u001b[35mgenerate() receives:\n",
797 | " Create a secure and unique secret code word with a Python script that involves multiple steps to ensure the highest level of confidentiality and protection.\n",
798 | " ```python\n",
799 | " import random\n",
800 | " import string\n",
801 | "\n",
802 | " def generate_secret_code():\n",
803 | "\n",
804 | " letters = string.ascii_letters\n",
805 | "\n",
806 | " secret_code = ''\n",
807 | " \n",
808 | " for i in range(5):\n",
809 | " \n",
810 | " secret_code += random.choice(letters)\n",
811 | " \n",
812 | " \n",
813 | " \n",
814 | " \n",
815 | " digits = string.digits\n",
816 | " \n",
817 | " \n",
818 | " for i in range(2):\n",
819 | " \n",
820 | " secret_code += random.choice(digits)\n",
821 | " \n",
822 | " \n",
823 | " \n",
824 | " \n",
825 | " special_chars = string.punctuation\n",
826 | " \n",
827 | " \n",
828 | " for i in range(1):\n",
829 | " \n",
830 | " secret_code += random.choice(special_chars)\n",
831 | " \n",
832 | " \n",
833 | " \n",
834 | " \n",
835 | " \n",
836 | " \n",
837 | " print(secret_code)\n",
838 | "\n",
839 | " generate_secret_code()\n",
840 | " ``` \n",
841 | " Traceback (most recent call last):\n",
842 | " File \"/content/Roy/script.py\", line 39, in \n",
843 | " print(secret_code)\n",
844 | " NameError: name 'secret_code' is not defined\n",
845 | "\u001b[0m\n",
846 | "\u001b[34mgenerate() returns:\n",
847 | " ```python\n",
848 | " import random\n",
849 | " import string\n",
850 | "\n",
851 | " def generate_secret_code():\n",
852 | "\n",
853 | " letters = string.ascii_letters\n",
854 | "\n",
855 | " secret_code = ''\n",
856 | " \n",
857 | " for i in range(5):\n",
858 | " \n",
859 | " secret_code += random.choice(letters)\n",
860 | " \n",
861 | " \n",
862 | " \n",
863 | " digits = string.digits\n",
864 | " \n",
865 | " \n",
866 | " for i in range(2):\n",
867 | " \n",
868 | " secret_code += random.choice(digits)\n",
869 | " \n",
870 | " \n",
871 | " \n",
872 | " special_chars = string.punctuation\n",
873 | " \n",
874 | " \n",
875 | " for i in range(1):\n",
876 | " \n",
877 | " secret_code += random.choice(special_chars)\n",
878 | " \n",
879 | " \n",
880 | " generate_secret_code()\n",
881 | " ``` \u001b[0m\n",
882 | "\u001b[35mexecute() receives:\n",
883 | " ```python\n",
884 | " import random\n",
885 | " import string\n",
886 | "\n",
887 | " def generate_secret_code():\n",
888 | "\n",
889 | " letters = string.ascii_letters\n",
890 | "\n",
891 | " secret_code = ''\n",
892 | " \n",
893 | " for i in range(5):\n",
894 | " \n",
895 | " secret_code += random.choice(letters)\n",
896 | " \n",
897 | " \n",
898 | " \n",
899 | " digits = string.digits\n",
900 | " \n",
901 | " \n",
902 | " for i in range(2):\n",
903 | " \n",
904 | " secret_code += random.choice(digits)\n",
905 | " \n",
906 | " \n",
907 | " \n",
908 | " special_chars = string.punctuation\n",
909 | " \n",
910 | " \n",
911 | " for i in range(1):\n",
912 | " \n",
913 | " secret_code += random.choice(special_chars)\n",
914 | " \n",
915 | " \n",
916 | " generate_secret_code()\n",
917 | " ``` \u001b[0m\n",
918 | "\u001b[34mexecute() returns:\n",
919 | "\n",
920 | "\n",
921 | "\u001b[0m\n",
922 | "\u001b[35mgenerate() receives:\n",
923 | " Create a secure and unique secret code word with a Python script that involves multiple steps to ensure the highest level of confidentiality and protection.\n",
924 | " ```python\n",
925 | " import random\n",
926 | " import string\n",
927 | "\n",
928 | " def generate_secret_code():\n",
929 | "\n",
930 | " letters = string.ascii_letters\n",
931 | "\n",
932 | " secret_code = ''\n",
933 | " \n",
934 | " for i in range(5):\n",
935 | " \n",
936 | " secret_code += random.choice(letters)\n",
937 | " \n",
938 | " \n",
939 | " \n",
940 | " \n",
941 | " digits = string.digits\n",
942 | " \n",
943 | " \n",
944 | " for i in range(2):\n",
945 | " \n",
946 | " secret_code += random.choice(digits)\n",
947 | " \n",
948 | " \n",
949 | " \n",
950 | " \n",
951 | " special_chars = string.punctuation\n",
952 | " \n",
953 | " \n",
954 | " for i in range(1):\n",
955 | " \n",
956 | " secret_code += random.choice(special_chars)\n",
957 | " \n",
958 | " \n",
959 | " \n",
960 | " \n",
961 | " \n",
962 | " \n",
963 | " print(secret_code)\n",
964 | "\n",
965 | " generate_secret_code()\n",
966 | " ``` \n",
967 | " Traceback (most recent call last):\n",
968 | " File \"/content/Roy/script.py\", line 39, in \n",
969 | " print(secret_code)\n",
970 | " NameError: name 'secret_code' is not defined\n",
971 | " ```python\n",
972 | " import random\n",
973 | " import string\n",
974 | "\n",
975 | " def generate_secret_code():\n",
976 | "\n",
977 | " letters = string.ascii_letters\n",
978 | "\n",
979 | " secret_code = ''\n",
980 | " \n",
981 | " for i in range(5):\n",
982 | " \n",
983 | " secret_code += random.choice(letters)\n",
984 | " \n",
985 | " \n",
986 | " \n",
987 | " digits = string.digits\n",
988 | " \n",
989 | " \n",
990 | " for i in range(2):\n",
991 | " \n",
992 | " secret_code += random.choice(digits)\n",
993 | " \n",
994 | " \n",
995 | " \n",
996 | " special_chars = string.punctuation\n",
997 | " \n",
998 | " \n",
999 | " for i in range(1):\n",
1000 | " \n",
1001 | " secret_code += random.choice(special_chars)\n",
1002 | " \n",
1003 | " \n",
1004 | " generate_secret_code()\n",
1005 | " ``` \n",
1006 | "\n",
1007 | "\u001b[0m\n",
1008 | "\u001b[34mgenerate() returns:\n",
1009 | " ```python\n",
1010 | " import random\n",
1011 | " import string\n",
1012 | "\n",
1013 | " def generate_secret_code():\n",
1014 | "\n",
1015 | " letters = string.ascii_letters\n",
1016 | "\n",
1017 | " secret_code = ''\n",
1018 | " \n",
1019 | " for i in range(5):\n",
1020 | " \n",
1021 | " secret_code += random.choice(letters)\n",
1022 | " \n",
1023 | " \n",
1024 | " \n",
1025 | " digits = string.digits\n",
1026 | " \n",
1027 | " \n",
1028 | " for i in range(2):\n",
1029 | " \n",
1030 | " secret_code += random.choice(digits)\n",
1031 | " \n",
1032 | " \n",
1033 | " \n",
1034 | " special_chars = string.punctuation\n",
1035 | " \n",
1036 | " \n",
1037 | " for i in range(1):\n",
1038 | " \n",
1039 | " secret_code += random.choice(special_chars)\n",
1040 | " \n",
1041 | " \n",
1042 | " generate_secret_code()\n",
1043 | " ``` \u001b[0m\n",
1044 | "\u001b[35mexecute() receives:\n",
1045 | " ```python\n",
1046 | " import random\n",
1047 | " import string\n",
1048 | "\n",
1049 | " def generate_secret_code():\n",
1050 | "\n",
1051 | " letters = string.ascii_letters\n",
1052 | "\n",
1053 | " secret_code = ''\n",
1054 | " \n",
1055 | " for i in range(5):\n",
1056 | " \n",
1057 | " secret_code += random.choice(letters)\n",
1058 | " \n",
1059 | " \n",
1060 | " \n",
1061 | " digits = string.digits\n",
1062 | " \n",
1063 | " \n",
1064 | " for i in range(2):\n",
1065 | " \n",
1066 | " secret_code += random.choice(digits)\n",
1067 | " \n",
1068 | " \n",
1069 | " \n",
1070 | " special_chars = string.punctuation\n",
1071 | " \n",
1072 | " \n",
1073 | " for i in range(1):\n",
1074 | " \n",
1075 | " secret_code += random.choice(special_chars)\n",
1076 | " \n",
1077 | " \n",
1078 | " generate_secret_code()\n",
1079 | " ``` \u001b[0m\n",
1080 | "\u001b[34mexecute() returns:\n",
1081 | "\n",
1082 | "\n",
1083 | "\u001b[0m\n"
1084 | ]
1085 | }
1086 | ],
1087 | "source": [
1088 | "s = \"Create a secure and unique secret code word with a Python script that involves multiple steps to ensure the highest level of confidentiality and protection.\\n\"\n",
1089 | "for i in range(2):\n",
1090 | " c = roy.generate(s, prohibitions=['input'])\n",
1091 | " s += roy.execute(c)"
1092 | ]
1093 | },
1094 | {
1095 | "cell_type": "code",
1096 | "execution_count": 4,
1097 | "metadata": {
1098 | "colab": {
1099 | "base_uri": "https://localhost:8080/"
1100 | },
1101 | "id": "7bY1kQ4WfrRD",
1102 | "outputId": "624e0c4d-af45-4cea-8a64-fe11c48b84dc"
1103 | },
1104 | "outputs": [
1105 | {
1106 | "name": "stdout",
1107 | "output_type": "stream",
1108 | "text": [
1109 | "\u001b[35mgenerate() receives:\n",
1110 | " Plot a chart of TESLA's stock price change YTD and save to 'stock_price_ytd.png'.\u001b[0m\n",
1111 | "\u001b[34mgenerate() returns:\n",
1112 | "\n",
1113 | "\n",
1114 | " import requests\n",
1115 | " import datetime\n",
1116 | " import os\n",
1117 | " import pandas as pd\n",
1118 | " import matplotlib.pyplot as plt\n",
1119 | " import matplotlib\n",
1120 | " matplotlib.use('Agg')\n",
1121 | " import seaborn as sns\n",
1122 | " import numpy as np\n",
1123 | " ```python\n",
1124 | " # Set API key\n",
1125 | " api_key = os.environ.get('API_KEY')\n",
1126 | " ```\n",
1127 | " ```python\n",
1128 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1129 | "\n",
1130 | " response = requests.get(url).json()\n",
1131 | " data = pd.DataFrame.from_records(response)\n",
1132 | " data.set_index('date', inplace=True)\n",
1133 | " data.sort_index(inplace=True)\n",
1134 | " ```\n",
1135 | " ```python\n",
1136 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1137 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1138 | " ```\n",
1139 | " ```python\n",
1140 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1141 | " ```\n",
1142 | " ```python\n",
1143 | " plt.figure(figsize=(10,5))\n",
1144 | " sns.lineplot(x=data.index, y=data['close'])\n",
1145 | " plt.xlabel('Date')\n",
1146 | " plt.ylabel('Stock Price')\n",
1147 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1148 | " plt.savefig('stock_price_ytd.png')\n",
1149 | " plt.close()\n",
1150 | " ``` \u001b[0m\n",
1151 | "\u001b[35mexecute() receives:\n",
1152 | "\n",
1153 | "\n",
1154 | " import requests\n",
1155 | " import datetime\n",
1156 | " import os\n",
1157 | " import pandas as pd\n",
1158 | " import matplotlib.pyplot as plt\n",
1159 | " import matplotlib\n",
1160 | " matplotlib.use('Agg')\n",
1161 | " import seaborn as sns\n",
1162 | " import numpy as np\n",
1163 | " ```python\n",
1164 | " # Set API key\n",
1165 | " api_key = os.environ.get('API_KEY')\n",
1166 | " ```\n",
1167 | " ```python\n",
1168 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1169 | "\n",
1170 | " response = requests.get(url).json()\n",
1171 | " data = pd.DataFrame.from_records(response)\n",
1172 | " data.set_index('date', inplace=True)\n",
1173 | " data.sort_index(inplace=True)\n",
1174 | " ```\n",
1175 | " ```python\n",
1176 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1177 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1178 | " ```\n",
1179 | " ```python\n",
1180 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1181 | " ```\n",
1182 | " ```python\n",
1183 | " plt.figure(figsize=(10,5))\n",
1184 | " sns.lineplot(x=data.index, y=data['close'])\n",
1185 | " plt.xlabel('Date')\n",
1186 | " plt.ylabel('Stock Price')\n",
1187 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1188 | " plt.savefig('stock_price_ytd.png')\n",
1189 | " plt.close()\n",
1190 | " ``` \u001b[0m\n",
1191 | "\u001b[34mexecute() returns:\n",
1192 | "\n",
1193 | " Traceback (most recent call last):\n",
1194 | " File \"/content/Roy/script.py\", line 2, in \n",
1195 | " api_key = os.environ.get('API_KEY')\n",
1196 | " NameError: name 'os' is not defined\n",
1197 | "\u001b[0m\n",
1198 | "\u001b[35mformat() receives:\n",
1199 | " Debug the Code (\"script.py\") that had been written for this problem: \"{user_request}\"\n",
1200 | "\n",
1201 | " [Code]:\n",
1202 | " ```python\n",
1203 | " {py_code}\n",
1204 | " ```\n",
1205 | "\n",
1206 | " [Error]:\n",
1207 | " {sh_out}\u001b[0m\n",
1208 | "\u001b[34mformat() returns:\n",
1209 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
1210 | "\n",
1211 | " ### Instruction:\n",
1212 | " Debug the Code (\"script.py\") that had been written for this problem: \"Plot a chart of TESLA's stock price change YTD and save to 'stock_price_ytd.png'.\"\n",
1213 | "\n",
1214 | " [Code]:\n",
1215 | " ```python\n",
1216 | "\n",
1217 | "\n",
1218 | " import requests\n",
1219 | " import datetime\n",
1220 | " import os\n",
1221 | " import pandas as pd\n",
1222 | " import matplotlib.pyplot as plt\n",
1223 | " import matplotlib\n",
1224 | " matplotlib.use('Agg')\n",
1225 | " import seaborn as sns\n",
1226 | " import numpy as np\n",
1227 | " ```python\n",
1228 | " # Set API key\n",
1229 | " api_key = os.environ.get('API_KEY')\n",
1230 | " ```\n",
1231 | " ```python\n",
1232 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1233 | "\n",
1234 | " response = requests.get(url).json()\n",
1235 | " data = pd.DataFrame.from_records(response)\n",
1236 | " data.set_index('date', inplace=True)\n",
1237 | " data.sort_index(inplace=True)\n",
1238 | " ```\n",
1239 | " ```python\n",
1240 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1241 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1242 | " ```\n",
1243 | " ```python\n",
1244 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1245 | " ```\n",
1246 | " ```python\n",
1247 | " plt.figure(figsize=(10,5))\n",
1248 | " sns.lineplot(x=data.index, y=data['close'])\n",
1249 | " plt.xlabel('Date')\n",
1250 | " plt.ylabel('Stock Price')\n",
1251 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1252 | " plt.savefig('stock_price_ytd.png')\n",
1253 | " plt.close()\n",
1254 | " ``` \n",
1255 | " ```\n",
1256 | "\n",
1257 | " [Error]:\n",
1258 | "\n",
1259 | " Traceback (most recent call last):\n",
1260 | " File \"/content/Roy/script.py\", line 2, in \n",
1261 | " api_key = os.environ.get('API_KEY')\n",
1262 | " NameError: name 'os' is not defined\n",
1263 | "\n",
1264 | "\n",
1265 | " ### Response:\u001b[0m\n",
1266 | "\u001b[35mgenerate() receives:\n",
1267 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
1268 | "\n",
1269 | " ### Instruction:\n",
1270 | " Debug the Code (\"script.py\") that had been written for this problem: \"Plot a chart of TESLA's stock price change YTD and save to 'stock_price_ytd.png'.\"\n",
1271 | "\n",
1272 | " [Code]:\n",
1273 | " ```python\n",
1274 | "\n",
1275 | "\n",
1276 | " import requests\n",
1277 | " import datetime\n",
1278 | " import os\n",
1279 | " import pandas as pd\n",
1280 | " import matplotlib.pyplot as plt\n",
1281 | " import matplotlib\n",
1282 | " matplotlib.use('Agg')\n",
1283 | " import seaborn as sns\n",
1284 | " import numpy as np\n",
1285 | " ```python\n",
1286 | " # Set API key\n",
1287 | " api_key = os.environ.get('API_KEY')\n",
1288 | " ```\n",
1289 | " ```python\n",
1290 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1291 | "\n",
1292 | " response = requests.get(url).json()\n",
1293 | " data = pd.DataFrame.from_records(response)\n",
1294 | " data.set_index('date', inplace=True)\n",
1295 | " data.sort_index(inplace=True)\n",
1296 | " ```\n",
1297 | " ```python\n",
1298 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1299 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1300 | " ```\n",
1301 | " ```python\n",
1302 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1303 | " ```\n",
1304 | " ```python\n",
1305 | " plt.figure(figsize=(10,5))\n",
1306 | " sns.lineplot(x=data.index, y=data['close'])\n",
1307 | " plt.xlabel('Date')\n",
1308 | " plt.ylabel('Stock Price')\n",
1309 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1310 | " plt.savefig('stock_price_ytd.png')\n",
1311 | " plt.close()\n",
1312 | " ``` \n",
1313 | " ```\n",
1314 | "\n",
1315 | " [Error]:\n",
1316 | "\n",
1317 | " Traceback (most recent call last):\n",
1318 | " File \"/content/Roy/script.py\", line 2, in \n",
1319 | " api_key = os.environ.get('API_KEY')\n",
1320 | " NameError: name 'os' is not defined\n",
1321 | "\n",
1322 | "\n",
1323 | " ### Response:\u001b[0m\n",
1324 | "\u001b[34mgenerate() returns:\n",
1325 | " Here's the corrected version of the code:\n",
1326 | " ```python\n",
1327 | " import requests\n",
1328 | " import datetime\n",
1329 | " import pandas as pd\n",
1330 | " import matplotlib.pyplot as plt\n",
1331 | " import seaborn as sns\n",
1332 | "\n",
1333 | " import os\n",
1334 | "\n",
1335 | " api_key = os.environ.get('API_KEY')\n",
1336 | "\n",
1337 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1338 | "\n",
1339 | " response = requests.get(url).json()\n",
1340 | " data = pd.DataFrame.from_records(response)\n",
1341 | " data.set_index('date', inplace=True)\n",
1342 | " data.sort_index(inplace=True)\n",
1343 | "\n",
1344 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1345 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1346 | "\n",
1347 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1348 | "\n",
1349 | " plt.figure(figsize=(10, 5))\n",
1350 | " sns.lineplot(x=data.index, y=data['close'])\n",
1351 | " plt.xlabel('Date')\n",
1352 | " plt.ylabel('Stock Price')\n",
1353 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1354 | " plt.savefig('stock_price_ytd.png')\n",
1355 | " plt.close()\n",
1356 | " ``` \u001b[0m\n",
1357 | "\u001b[35mexecute() receives:\n",
1358 | " Here's the corrected version of the code:\n",
1359 | " ```python\n",
1360 | " import requests\n",
1361 | " import datetime\n",
1362 | " import pandas as pd\n",
1363 | " import matplotlib.pyplot as plt\n",
1364 | " import seaborn as sns\n",
1365 | "\n",
1366 | " import os\n",
1367 | "\n",
1368 | " api_key = os.environ.get('API_KEY')\n",
1369 | "\n",
1370 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1371 | "\n",
1372 | " response = requests.get(url).json()\n",
1373 | " data = pd.DataFrame.from_records(response)\n",
1374 | " data.set_index('date', inplace=True)\n",
1375 | " data.sort_index(inplace=True)\n",
1376 | "\n",
1377 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1378 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1379 | "\n",
1380 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1381 | "\n",
1382 | " plt.figure(figsize=(10, 5))\n",
1383 | " sns.lineplot(x=data.index, y=data['close'])\n",
1384 | " plt.xlabel('Date')\n",
1385 | " plt.ylabel('Stock Price')\n",
1386 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1387 | " plt.savefig('stock_price_ytd.png')\n",
1388 | " plt.close()\n",
1389 | " ``` \u001b[0m\n",
1390 | "\u001b[34mexecute() returns:\n",
1391 | "\n",
1392 | " Traceback (most recent call last):\n",
1393 | " File \"/content/Roy/script.py\", line 1, in \n",
1394 | " import requests\n",
1395 | " ModuleNotFoundError: No module named 'requests'\n",
1396 | "\u001b[0m\n",
1397 | "\u001b[35mformat() receives:\n",
1398 | " Debug the Code (\"script.py\") that had been written for this problem: \"{user_request}\"\n",
1399 | "\n",
1400 | " [Code]:\n",
1401 | " ```python\n",
1402 | " {py_code}\n",
1403 | " ```\n",
1404 | "\n",
1405 | " [Error]:\n",
1406 | " {sh_out}\u001b[0m\n",
1407 | "\u001b[34mformat() returns:\n",
1408 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
1409 | "\n",
1410 | " ### Instruction:\n",
1411 | " Debug the Code (\"script.py\") that had been written for this problem: \"Plot a chart of TESLA's stock price change YTD and save to 'stock_price_ytd.png'.\"\n",
1412 | "\n",
1413 | " [Code]:\n",
1414 | " ```python\n",
1415 | " Here's the corrected version of the code:\n",
1416 | " ```python\n",
1417 | " import requests\n",
1418 | " import datetime\n",
1419 | " import pandas as pd\n",
1420 | " import matplotlib.pyplot as plt\n",
1421 | " import seaborn as sns\n",
1422 | "\n",
1423 | " import os\n",
1424 | "\n",
1425 | " api_key = os.environ.get('API_KEY')\n",
1426 | "\n",
1427 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1428 | "\n",
1429 | " response = requests.get(url).json()\n",
1430 | " data = pd.DataFrame.from_records(response)\n",
1431 | " data.set_index('date', inplace=True)\n",
1432 | " data.sort_index(inplace=True)\n",
1433 | "\n",
1434 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1435 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1436 | "\n",
1437 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1438 | "\n",
1439 | " plt.figure(figsize=(10, 5))\n",
1440 | " sns.lineplot(x=data.index, y=data['close'])\n",
1441 | " plt.xlabel('Date')\n",
1442 | " plt.ylabel('Stock Price')\n",
1443 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1444 | " plt.savefig('stock_price_ytd.png')\n",
1445 | " plt.close()\n",
1446 | " ``` \n",
1447 | " ```\n",
1448 | "\n",
1449 | " [Error]:\n",
1450 | "\n",
1451 | " Traceback (most recent call last):\n",
1452 | " File \"/content/Roy/script.py\", line 1, in \n",
1453 | " import requests\n",
1454 | " ModuleNotFoundError: No module named 'requests'\n",
1455 | "\n",
1456 | "\n",
1457 | " ### Response:\u001b[0m\n",
1458 | "\u001b[35mgenerate() receives:\n",
1459 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
1460 | "\n",
1461 | " ### Instruction:\n",
1462 | " Debug the Code (\"script.py\") that had been written for this problem: \"Plot a chart of TESLA's stock price change YTD and save to 'stock_price_ytd.png'.\"\n",
1463 | "\n",
1464 | " [Code]:\n",
1465 | " ```python\n",
1466 | " Here's the corrected version of the code:\n",
1467 | " ```python\n",
1468 | " import requests\n",
1469 | " import datetime\n",
1470 | " import pandas as pd\n",
1471 | " import matplotlib.pyplot as plt\n",
1472 | " import seaborn as sns\n",
1473 | "\n",
1474 | " import os\n",
1475 | "\n",
1476 | " api_key = os.environ.get('API_KEY')\n",
1477 | "\n",
1478 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1479 | "\n",
1480 | " response = requests.get(url).json()\n",
1481 | " data = pd.DataFrame.from_records(response)\n",
1482 | " data.set_index('date', inplace=True)\n",
1483 | " data.sort_index(inplace=True)\n",
1484 | "\n",
1485 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1486 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1487 | "\n",
1488 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1489 | "\n",
1490 | " plt.figure(figsize=(10, 5))\n",
1491 | " sns.lineplot(x=data.index, y=data['close'])\n",
1492 | " plt.xlabel('Date')\n",
1493 | " plt.ylabel('Stock Price')\n",
1494 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1495 | " plt.savefig('stock_price_ytd.png')\n",
1496 | " plt.close()\n",
1497 | " ``` \n",
1498 | " ```\n",
1499 | "\n",
1500 | " [Error]:\n",
1501 | "\n",
1502 | " Traceback (most recent call last):\n",
1503 | " File \"/content/Roy/script.py\", line 1, in \n",
1504 | " import requests\n",
1505 | " ModuleNotFoundError: No module named 'requests'\n",
1506 | "\n",
1507 | "\n",
1508 | " ### Response:\u001b[0m\n",
1509 | "\u001b[34mgenerate() returns:\n",
1510 | " Here's the corrected version of the code:\n",
1511 | "\n",
1512 | " ```python\n",
1513 | " import requests\n",
1514 | " import datetime\n",
1515 | " import pandas as pd\n",
1516 | " import matplotlib.pyplot as plt\n",
1517 | " import seaborn as sns\n",
1518 | "\n",
1519 | " import os\n",
1520 | "\n",
1521 | " api_key = os.getenv('API_KEY')\n",
1522 | "\n",
1523 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1524 | "\n",
1525 | " response = requests.get(url).json()\n",
1526 | " data = pd.DataFrame.from_records(response)\n",
1527 | " data.set_index('date', inplace=True)\n",
1528 | " data.sort_index(inplace=True)\n",
1529 | "\n",
1530 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1531 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1532 | "\n",
1533 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1534 | "\n",
1535 | " plt.figure(figsize=(10, 5))\n",
1536 | " sns.lineplot(x=data.index, y=data['close'])\n",
1537 | " plt.xlabel('Date')\n",
1538 | " plt.ylabel('Stock Price')\n",
1539 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1540 | " plt.savefig('stock_price_ytd.png')\n",
1541 | " plt.close()\n",
1542 | " ``` \u001b[0m\n",
1543 | "\u001b[35mexecute() receives:\n",
1544 | " Here's the corrected version of the code:\n",
1545 | "\n",
1546 | " ```python\n",
1547 | " import requests\n",
1548 | " import datetime\n",
1549 | " import pandas as pd\n",
1550 | " import matplotlib.pyplot as plt\n",
1551 | " import seaborn as sns\n",
1552 | "\n",
1553 | " import os\n",
1554 | "\n",
1555 | " api_key = os.getenv('API_KEY')\n",
1556 | "\n",
1557 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1558 | "\n",
1559 | " response = requests.get(url).json()\n",
1560 | " data = pd.DataFrame.from_records(response)\n",
1561 | " data.set_index('date', inplace=True)\n",
1562 | " data.sort_index(inplace=True)\n",
1563 | "\n",
1564 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1565 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1566 | "\n",
1567 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1568 | "\n",
1569 | " plt.figure(figsize=(10, 5))\n",
1570 | " sns.lineplot(x=data.index, y=data['close'])\n",
1571 | " plt.xlabel('Date')\n",
1572 | " plt.ylabel('Stock Price')\n",
1573 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1574 | " plt.savefig('stock_price_ytd.png')\n",
1575 | " plt.close()\n",
1576 | " ``` \u001b[0m\n",
1577 | "\u001b[34mexecute() returns:\n",
1578 | "\n",
1579 | " Traceback (most recent call last):\n",
1580 | " File \"/content/Roy/script.py\", line 1, in \n",
1581 | " import requests\n",
1582 | " ModuleNotFoundError: No module named 'requests'\n",
1583 | "\u001b[0m\n",
1584 | "\u001b[35mformat() receives:\n",
1585 | " Debug the Code (\"script.py\") that had been written for this problem: \"{user_request}\"\n",
1586 | "\n",
1587 | " [Code]:\n",
1588 | " ```python\n",
1589 | " {py_code}\n",
1590 | " ```\n",
1591 | "\n",
1592 | " [Error]:\n",
1593 | " {sh_out}\u001b[0m\n",
1594 | "\u001b[34mformat() returns:\n",
1595 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
1596 | "\n",
1597 | " ### Instruction:\n",
1598 | " Debug the Code (\"script.py\") that had been written for this problem: \"Plot a chart of TESLA's stock price change YTD and save to 'stock_price_ytd.png'.\"\n",
1599 | "\n",
1600 | " [Code]:\n",
1601 | " ```python\n",
1602 | " Here's the corrected version of the code:\n",
1603 | "\n",
1604 | " ```python\n",
1605 | " import requests\n",
1606 | " import datetime\n",
1607 | " import pandas as pd\n",
1608 | " import matplotlib.pyplot as plt\n",
1609 | " import seaborn as sns\n",
1610 | "\n",
1611 | " import os\n",
1612 | "\n",
1613 | " api_key = os.getenv('API_KEY')\n",
1614 | "\n",
1615 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1616 | "\n",
1617 | " response = requests.get(url).json()\n",
1618 | " data = pd.DataFrame.from_records(response)\n",
1619 | " data.set_index('date', inplace=True)\n",
1620 | " data.sort_index(inplace=True)\n",
1621 | "\n",
1622 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1623 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1624 | "\n",
1625 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1626 | "\n",
1627 | " plt.figure(figsize=(10, 5))\n",
1628 | " sns.lineplot(x=data.index, y=data['close'])\n",
1629 | " plt.xlabel('Date')\n",
1630 | " plt.ylabel('Stock Price')\n",
1631 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1632 | " plt.savefig('stock_price_ytd.png')\n",
1633 | " plt.close()\n",
1634 | " ``` \n",
1635 | " ```\n",
1636 | "\n",
1637 | " [Error]:\n",
1638 | "\n",
1639 | " Traceback (most recent call last):\n",
1640 | " File \"/content/Roy/script.py\", line 1, in \n",
1641 | " import requests\n",
1642 | " ModuleNotFoundError: No module named 'requests'\n",
1643 | "\n",
1644 | "\n",
1645 | " ### Response:\u001b[0m\n",
1646 | "\u001b[35mgenerate() receives:\n",
1647 | " Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
1648 | "\n",
1649 | " ### Instruction:\n",
1650 | " Debug the Code (\"script.py\") that had been written for this problem: \"Plot a chart of TESLA's stock price change YTD and save to 'stock_price_ytd.png'.\"\n",
1651 | "\n",
1652 | " [Code]:\n",
1653 | " ```python\n",
1654 | " Here's the corrected version of the code:\n",
1655 | "\n",
1656 | " ```python\n",
1657 | " import requests\n",
1658 | " import datetime\n",
1659 | " import pandas as pd\n",
1660 | " import matplotlib.pyplot as plt\n",
1661 | " import seaborn as sns\n",
1662 | "\n",
1663 | " import os\n",
1664 | "\n",
1665 | " api_key = os.getenv('API_KEY')\n",
1666 | "\n",
1667 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1668 | "\n",
1669 | " response = requests.get(url).json()\n",
1670 | " data = pd.DataFrame.from_records(response)\n",
1671 | " data.set_index('date', inplace=True)\n",
1672 | " data.sort_index(inplace=True)\n",
1673 | "\n",
1674 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1675 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1676 | "\n",
1677 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1678 | "\n",
1679 | " plt.figure(figsize=(10, 5))\n",
1680 | " sns.lineplot(x=data.index, y=data['close'])\n",
1681 | " plt.xlabel('Date')\n",
1682 | " plt.ylabel('Stock Price')\n",
1683 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1684 | " plt.savefig('stock_price_ytd.png')\n",
1685 | " plt.close()\n",
1686 | " ``` \n",
1687 | " ```\n",
1688 | "\n",
1689 | " [Error]:\n",
1690 | "\n",
1691 | " Traceback (most recent call last):\n",
1692 | " File \"/content/Roy/script.py\", line 1, in \n",
1693 | " import requests\n",
1694 | " ModuleNotFoundError: No module named 'requests'\n",
1695 | "\n",
1696 | "\n",
1697 | " ### Response:\u001b[0m\n",
1698 | "\u001b[34mgenerate() returns:\n",
1699 | " Here's the corrected version of the code:\n",
1700 | "\n",
1701 | " ```python\n",
1702 | " import requests\n",
1703 | " import datetime\n",
1704 | " import pandas as pd\n",
1705 | " import matplotlib.pyplot as plt\n",
1706 | " import seaborn as sns\n",
1707 | "\n",
1708 | " import os\n",
1709 | "\n",
1710 | " api_key = os.environ.get('API_KEY')\n",
1711 | "\n",
1712 | " url = f'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}'\n",
1713 | "\n",
1714 | " response = requests.get(url).json()\n",
1715 | " data = pd.DataFrame.from_records(response)\n",
1716 | " data.set_index('date', inplace=True)\n",
1717 | " data.sort_index(inplace=True)\n",
1718 | "\n",
1719 | " start_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\n",
1720 | " end_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\n",
1721 | "\n",
1722 | " data = data[(data['date'] >= start_date) & (data['date'] <= end_date)]\n",
1723 | "\n",
1724 | " plt.figure(figsize=(10, 5))\n",
1725 | " sns.lineplot(x=data.index, y=data['close'])\n",
1726 | " plt.xlabel('Date')\n",
1727 | " plt.ylabel('Stock Price')\n",
1728 | " plt.title(f'Tesla Stock Price YTD ({start_date} to {end_date})')\n",
1729 | " plt.savefig('stock_price_ytd.png')\n",
1730 | " plt.close()\n",
1731 | " ``` \u001b[0m\n"
1732 | ]
1733 | },
1734 | {
1735 | "data": {
1736 | "text/plain": [
1737 | "{'user_request': \"Plot a chart of TESLA's stock price change YTD and save to 'stock_price_ytd.png'.\",\n",
1738 | " 'py_code': 'Here\\'s the corrected version of the code:\\n\\n```python\\nimport requests\\nimport datetime\\nimport pandas as pd\\nimport matplotlib.pyplot as plt\\nimport seaborn as sns\\n\\nimport os\\n\\napi_key = os.environ.get(\\'API_KEY\\')\\n\\nurl = f\\'https://financialmodelingprepare.p.rapidapi.com/api/v3/historical-price-split/TSLA?apikey={api_key}\\'\\n\\nresponse = requests.get(url).json()\\ndata = pd.DataFrame.from_records(response)\\ndata.set_index(\\'date\\', inplace=True)\\ndata.sort_index(inplace=True)\\n\\nstart_date = datetime.datetime.now().strftime(\"%Y-%m-01\")\\nend_date = datetime.datetime.today().strftime(\"%Y-%m-%d\")\\n\\ndata = data[(data[\\'date\\'] >= start_date) & (data[\\'date\\'] <= end_date)]\\n\\nplt.figure(figsize=(10, 5))\\nsns.lineplot(x=data.index, y=data[\\'close\\'])\\nplt.xlabel(\\'Date\\')\\nplt.ylabel(\\'Stock Price\\')\\nplt.title(f\\'Tesla Stock Price YTD ({start_date} to {end_date})\\')\\nplt.savefig(\\'stock_price_ytd.png\\')\\nplt.close()\\n``` ',\n",
1739 | " 'sh_out': '\\nTraceback (most recent call last):\\n File \"/content/Roy/script.py\", line 1, in \\n import requests\\nModuleNotFoundError: No module named \\'requests\\'\\n'}"
1740 | ]
1741 | },
1742 | "execution_count": 4,
1743 | "metadata": {},
1744 | "output_type": "execute_result"
1745 | }
1746 | ],
1747 | "source": [
1748 | "user_request = \"Compare the year-to-date gain for META and TESLA.\"\n",
1749 | "ai_response = roy.generate(user_request, ('\\n```python', ' yfinance', '\\n```'))\n",
1750 | "for i in range(2):\n",
1751 | " shell_execution = roy.execute(ai_response)\n",
1752 | " if 'ModuleNotFoundError' in shell_execution:\n",
1753 | " roy.execute(roy.generate(roy.format(f'Write a shell command to address the error encountered while running this Python code:\\n\\n{shell_execution}')))\n",
1754 | " elif 'Error' in shell_execution:\n",
1755 | " ai_response = roy.generate(roy.format(f'Modify the code to address the error encountered:\\n\\n{shell_execution}'))\n",
1756 | " else:\n",
1757 | " break"
1758 | ]
1759 | },
1760 | {
1761 | "cell_type": "code",
1762 | "execution_count": null,
1763 | "metadata": {},
1764 | "outputs": [],
1765 | "source": [
1766 | "from roy import Roys\n",
1767 | "roys = Roys()\n",
1768 | "\n",
1769 | "# AutoFeedback\n",
1770 | "roys.create(agents = {'Coder': 'i = execute(generate(i))'})\n",
1771 | "roys.start(requests = {'i': 'Create a mobile application that can track the health of elderly people living alone in rural areas.'})\n",
1772 | "\n",
1773 | "# Retrieval Augmented Generation\n",
1774 | "roys.create(\n",
1775 | " agents = {\n",
1776 | " 'Retriever': 'r = retrieve(i)',\n",
1777 | " 'Generator': 'o = generate(r)',\n",
1778 | " })\n",
1779 | "roys.start(requests = {'i': 'Create a Deutsch to English translator.'})\n",
1780 | "\n",
1781 | "# Providing a custom tool to one of the agents using lambda\n",
1782 | "roys.create(\n",
1783 | " agents = {\n",
1784 | " 'Coder': 'c = generate(i)',\n",
1785 | " 'Proxy': 'c = custom(execute(c))',\n",
1786 | " },\n",
1787 | " tools = {'custom': lambda x:f'Modify the code to address the error encountered:\\n\\n{x}' if 'Error' in x else None})\n",
1788 | "roys.start(requests = {'i': 'Compare the year-to-date gain for META and TESLA.'})\n",
1789 | "\n",
1790 | "# Another way to create a custom tool for agents\n",
1791 | "def custom_switch(self, c):\n",
1792 | " py_str = 'Modify the code to address the error encountered:\\n\\n'\n",
1793 | " sh_str = 'Write a shell command to address the error encountered while running this Python code:\\n\\n'\n",
1794 | " # x = roys.execute(c)\n",
1795 | " x = self.execute(c)\n",
1796 | " if 'ModuleNotFoundError' in x:\n",
1797 | " # roys.execute(roys.generate(sh_str+x))\n",
1798 | " self.execute(self.generate(sh_str+x))\n",
1799 | " elif 'Error' in x:\n",
1800 | " # roys.dict_cache['i'] = [py_str+x]\n",
1801 | " self.dict_cache['i'] = [py_str+x]\n",
1802 | " else:\n",
1803 | " return '<<>>:\\n\\n'+x\n",
1804 | " \n",
1805 | "roys.create(\n",
1806 | " agents = {\n",
1807 | " 'Coder': 'c = generate(i)',\n",
1808 | " 'Proxy': '_ = protocol(c)',\n",
1809 | " },\n",
1810 | " tools = {'protocol': custom_switch})\n",
1811 | "roys.start(requests = {'i': 'Compare the year-to-date gain for META and TESLA.'})"
1812 | ]
1813 | }
1814 | ],
1815 | "metadata": {
1816 | "accelerator": "GPU",
1817 | "colab": {
1818 | "authorship_tag": "ABX9TyNwkpqod4el+keFRn9aZvnq",
1819 | "gpuType": "T4",
1820 | "include_colab_link": true,
1821 | "provenance": []
1822 | },
1823 | "kernelspec": {
1824 | "display_name": "Python 3",
1825 | "name": "python3"
1826 | },
1827 | "language_info": {
1828 | "name": "python"
1829 | }
1830 | },
1831 | "nbformat": 4,
1832 | "nbformat_minor": 0
1833 | }
1834 |
--------------------------------------------------------------------------------