Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat directly in cells by using input transformers. #1

Open
kolibril13 opened this issue Feb 19, 2023 · 0 comments
Open

Chat directly in cells by using input transformers. #1

kolibril13 opened this issue Feb 19, 2023 · 0 comments

Comments

@kolibril13
Copy link

kolibril13 commented Feb 19, 2023

Thanks for creating jupytee, this is an impressive tool!
And I want to suggest an idea:
The chat could happen directly in cells with the following code pattern.

import matplotlib.pyplot as plt
plt.plot([1,2], [10,20], c = "orange") # @chat change the color to green

Like this, one can use already existing python code with an extra # @chat in the end of a line, no cell magic needed.
Here is how a prototype could look like (made with a drawing tool):

image

How to get there?

First challenge: Find a way for the AI to understand that it should manipulate a certain line. That is straight forward:

image

Second challenge:
Find a way to write an IPython input transformer that does the following things:

  • takes the code
  • extracts the line that contains # @chat"
  • creates the request message from the screenshot above.

That can be done like this:

def run_with_gpt_listener(lines):
    contains_gpt_call = False
    my_message = ""
    for num, line in enumerate(lines):
        if "# @chat" in line:
            _ , new_suggestion = line.split("# @chat") 
            contains_gpt_call = True
            my_message += f"take the following code and make the following change to line {num+1}:"
            my_message += new_suggestion
            my_message += "\n"
            
    my_message += "```python\n"
    
    for line in lines:
        if "# @chat" in line:
            code_without_comment , _ = line.split("# @chat") 
            l = code_without_comment
        else:
            l = line
        
        my_message += l
        
    my_message += "\n"
    my_message += "```"
    print(my_message)
    return lines

ip = get_ipython()
ip.input_transformers_cleanup.append(run_with_gpt_listener)

and can be used like this:

image



Third challenge

Sent the request to OpenAI and print its response.
I did not implement that, but this request can be sent the same way it's also sent in the %%codemagic:

jupytee/jupytee/jupytee.py

Lines 117 to 159 in 3e19b0e

def code(self, line, cell=None):
"""Prompt for code generation.
The prompt can be purely an instruction (in natural language), or it can consist of both an instruction and a block of code. If you want to provide code, it should come below the instruction, after a separator (which defaults to `##` but can be set with the `--sep` argumet).
The code is displayed as syntax-highlighted Markdown. If you want to get the code directly inserted into a cell for later execution, call the `%get_code` magic.
Example:
```
%%code
Please add a docstring to this function.
##
def add(a, b): return a+b
```
"""
args = parse_argstring(self.code, line)
input = ""
if cell is None:
instruction = ' '.join(args.prompt)
else:
parts = cell.split(args.sep)
if len(parts) == 1:
instruction = cell
elif len(parts) == 2:
instruction, input = parts
else:
print("Only one ## marker is supported", file=sys.stderr)
return
if not(input) and "CODE" in instruction:
input = self.last_code
response = get_code_completion(instruction, input,
temperature=args.temp)
if response:
self.last_code = response.choices[0].text.strip()
return Markdown(f"```{args.lang}\n{self.last_code}\n```")
else:
print("ERROR: No response returned by GPT. Please try again.",
file=sys.stderr)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant