Skip to content

Commit

Permalink
black formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
patillacode committed Jun 15, 2023
1 parent 12b280f commit f4eb1e1
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 41 deletions.
15 changes: 6 additions & 9 deletions ai.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import openai


Expand All @@ -8,15 +7,15 @@ def __init__(self, **kwargs):

def start(self, system, user):
messages = [
{"role": "system", "content": system},
{"role": "user", "content": user},
]
{"role": "system", "content": system},
{"role": "user", "content": user},
]

return self.next(messages)

def fsystem(self, msg):
return {"role": "system", "content": msg}

def fuser(self, msg):
return {"role": "user", "content": msg}

Expand All @@ -25,9 +24,7 @@ def next(self, messages: list[dict[str, str]], prompt=None):
messages = messages + [{"role": "user", "content": prompt}]

response = openai.ChatCompletion.create(
messages=messages,
stream=True,
**self.kwargs
messages=messages, stream=True, **self.kwargs
)

chat = []
Expand All @@ -36,4 +33,4 @@ def next(self, messages: list[dict[str, str]], prompt=None):
msg = delta.get('content', '')
print(msg, end="")
chat.append(msg)
return messages + [{"role": "assistant", "content": "".join(chat)}]
return messages + [{"role": "assistant", "content": "".join(chat)}]
6 changes: 3 additions & 3 deletions chat_to_files.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re


def parse_chat(chat):# -> List[Tuple[str, str]]:
def parse_chat(chat): # -> List[Tuple[str, str]]:
# Get all ``` blocks
regex = r"```(.*?)```"

Expand All @@ -15,7 +15,7 @@ def parse_chat(chat):# -> List[Tuple[str, str]]:
code = "\n".join(code)
# Add the file to the list
files.append((path, code))

return files


Expand All @@ -24,4 +24,4 @@ def to_files(chat, workspace):

files = parse_chat(chat)
for file_name, file_content in files:
workspace[file_name] = file_content
workspace[file_name] = file_content
4 changes: 2 additions & 2 deletions db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from dataclasses import dataclass
import os
from pathlib import Path


Expand All @@ -25,4 +25,4 @@ class DBs:
logs: DB
identity: DB
input: DB
workspace: DB
workspace: DB
18 changes: 8 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import json
import os
import pathlib
from typing import Optional
import openai
from chat_to_files import to_files
from ai import AI
from steps import STEPS
from db import DB, DBs

import typer

from ai import AI
from db import DB, DBs
from steps import STEPS

app = typer.Typer()


@app.command()
def chat(
project_path: str = typer.Argument(None, help="path"),
run_prefix: str = typer.Option("", help="run prefix, if you want to run multiple variants of the same project and later compare them"),
run_prefix: str = typer.Option(
"",
help="run prefix, if you want to run multiple variants of the same project and later compare them",
),
model: str = "gpt-4",
temperature: float = 0.1,
):

if project_path is None:
project_path = str(pathlib.Path(__file__).parent / "example")

Expand All @@ -41,7 +40,6 @@ def chat(
identity=DB(pathlib.Path(__file__).parent / "identity"),
)


for step in STEPS:
messages = step(ai, dbs)
dbs.logs[step.__name__] = json.dumps(messages)
Expand Down
1 change: 0 additions & 1 deletion scripts/rerun_edited_message_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def chat(
temperature: float = 0.1,
max_tokens: int = 4096,
):

ai = AI(
model=model,
temperature=temperature,
Expand Down
35 changes: 19 additions & 16 deletions steps.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import json

from ai import AI
from chat_to_files import to_files
from db import DBs
import json


def setup_sys_prompt(dbs):
return dbs.identity['setup'] + '\nUseful to know:\n' + dbs.identity['philosophy']


def run(ai: AI, dbs: DBs):
'''Run the AI on the main prompt and save the results'''
messages = ai.start(setup_sys_prompt(dbs), dbs.input['main_prompt'])
messages = ai.start(
setup_sys_prompt(dbs),
dbs.input['main_prompt'],
)
to_files(messages[-1]['content'], dbs.workspace)
return messages


def clarify(ai: AI, dbs: DBs):
'''Ask the user if they want to clarify anything and save the results to the workspace'''
'''
Ask the user if they want to clarify anything and save the results to the workspace
'''
messages = [ai.fsystem(dbs.identity['qa'])]
user = dbs.input['main_prompt']
while True:
Expand All @@ -31,35 +39,30 @@ def clarify(ai: AI, dbs: DBs):
break

user += (
'\n\n'
'Is anything else unclear? If yes, only answer in the form:\n'
'\n\n'
'Is anything else unclear? If yes, only answer in the form:\n'
'{remaining unclear areas} remaining questions.\n'
'{Next question}\n'
'If everything is sufficiently clear, only answer "no".'
)
)

print()
return messages


def run_clarified(ai: AI, dbs: DBs):
# get the messages from previous step
messages = json.loads(dbs.logs[clarify.__name__])

messages = (
[
ai.fsystem(setup_sys_prompt(dbs)),
] +
messages[1:]
)
messages = [
ai.fsystem(setup_sys_prompt(dbs)),
] + messages[1:]
messages = ai.next(messages, dbs.identity['use_qa'])
to_files(messages[-1]['content'], dbs.workspace)
return messages


STEPS=[
clarify,
run_clarified
]
STEPS = [clarify, run_clarified]

# Future steps that can be added:
# improve_files,
Expand Down

0 comments on commit f4eb1e1

Please sign in to comment.