Skip to content

Commit

Permalink
streaming + clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
patillacode committed Nov 29, 2023
1 parent 794c215 commit 023e71d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
31 changes: 11 additions & 20 deletions chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,18 @@ def start_chat(model, verbose=False):
display_output("Starting chat...")

system = {"role": "system", "content": f"DIRECTIVE_FOR_{model}"}
# message = {"role": "user", "content": ""}
conversation = [system]
tokens = 0
console = Console()

completion = client.chat.completions.create(
model=model,
messages=conversation,
# stream=True,
)
# for chunk in completion:
# print(chunk.choices[0].delta)

while True:
prompt = input("\n􀳾 > ")

if prompt == "":
if prompt.strip() == "":
continue
elif prompt == "exit":
break
Expand All @@ -47,21 +42,17 @@ def start_chat(model, verbose=False):
completion = client.chat.completions.create(
model=model,
messages=conversation,
# stream=True,
stream=True,
)
# for chunk in completion:
# if chunk.choices[0].delta.content is not None:
# print(chunk.choices[0].delta.content, end="")
message = completion.choices[0].message
conversation.append({"role": "system", "content": message.content})
tokens = completion.usage.total_tokens

display_output("\n􀪬 > ", end="")
lines = split_message(message.content)
for line in lines:
display_output(f"{line}", color="yellow")

display_output(f"\n\n􀪬 ({tokens})", color="magenta")
messages = []
for chunk in completion:
if chunk.choices[0].delta.content is not None:
content = chunk.choices[0].delta.content
messages.append(content)
display_output(content, color="yellow", end="")

full_message = "".join(messages)
conversation.append({"role": "system", "content": full_message})

except Exception as e:
handle_error(e, verbose)
23 changes: 23 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from dotenv import load_dotenv
from icecream import ic # noqa: F401
from openai import OpenAI

load_dotenv()

client = OpenAI()

stream = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "Say this is a test, write a 20 word text poem about it.",
}
],
stream=True,
)
ic(stream)
for chunk in stream:
ic(chunk)
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")

0 comments on commit 023e71d

Please sign in to comment.