Ali Kadhim commited on
Commit
2f6ea1f
1 Parent(s): 1f87be8

Add core files

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