Spaces:
Sleeping
Sleeping
File size: 1,172 Bytes
2f6ea1f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
# OpenAI Chat completion
import openai
import chainlit as cl
# You only need the api key inserted here if it's not in your .env file
#openai.api_key = "YOUR_API_KEY"
model_name = "gpt-3.5-turbo"
settings = {
"temperature": 0.7,
"max_tokens": 500,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
}
@cl.on_chat_start
def start_chat():
cl.user_session.set(
"message_history",
[{"role": "system", "content": "You are a helpful assistant."}],
)
@cl.on_message
async def main(message: str):
message_history = cl.user_session.get("message_history")
message_history.append({"role": "user", "content": message})
msg = cl.Message(content="")
async for stream_resp in await openai.ChatCompletion.acreate(
model=model_name, messages=message_history, stream=True, **settings
):
token = stream_resp.choices[0]["delta"].get("content", "")
await msg.stream_token(token)
message_history.append({"role": "assistant", "content": msg.content})
await msg.send()
|