hamza82 commited on
Commit
f618ace
·
verified ·
1 Parent(s): 372e659

Update backend/app.py

Browse files
Files changed (1) hide show
  1. backend/app.py +53 -53
backend/app.py CHANGED
@@ -1,54 +1,54 @@
1
- import os
2
- from openai import AsyncOpenAI
3
-
4
- from fastapi.responses import JSONResponse
5
-
6
- from chainlit.auth import create_jwt
7
- from chainlit.server import app
8
- import chainlit as cl
9
-
10
-
11
- client = AsyncOpenAI(api_key=os.environ["OPENAI_API_KEY"])
12
-
13
- settings = {
14
- "model": "gpt-3.5-turbo",
15
- "temperature": 0.7,
16
- "max_tokens": 500,
17
- "top_p": 1,
18
- "frequency_penalty": 0,
19
- "presence_penalty": 0,
20
- }
21
-
22
- @app.get("/custom-auth")
23
- async def custom_auth():
24
- # Verify the user's identity with custom logic.
25
- token = create_jwt(cl.User(identifier="Test User"))
26
- return JSONResponse({"token": token})
27
-
28
- @cl.on_chat_start
29
- async def on_chat_start():
30
- cl.user_session.set(
31
- "message_history",
32
- [{"role": "system", "content": "You are a helpful assistant."}],
33
- )
34
- await cl.Message(content="Connected to Chainlit!").send()
35
-
36
-
37
- @cl.on_message
38
- async def on_message(message: cl.Message):
39
- message_history = cl.user_session.get("message_history")
40
- message_history.append({"role": "user", "content": message.content})
41
-
42
- msg = cl.Message(content="")
43
- await msg.send()
44
-
45
- stream = await client.chat.completions.create(
46
- messages=message_history, stream=True, **settings
47
- )
48
-
49
- async for part in stream:
50
- if token := part.choices[0].delta.content or "":
51
- await msg.stream_token(token)
52
-
53
- message_history.append({"role": "assistant", "content": msg.content})
54
  await msg.update()
 
1
+ import os
2
+ from openai import AsyncOpenAI
3
+
4
+ from fastapi.responses import JSONResponse
5
+
6
+ from chainlit.auth import create_jwt
7
+ from chainlit.server import app
8
+ import chainlit as cl
9
+
10
+
11
+ client = AsyncOpenAI(api_key=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)
12
+
13
+ settings = {
14
+ "model": "gpt-3.5-turbo",
15
+ "temperature": 0.7,
16
+ "max_tokens": 500,
17
+ "top_p": 1,
18
+ "frequency_penalty": 0,
19
+ "presence_penalty": 0,
20
+ }
21
+
22
+ @app.get("/custom-auth")
23
+ async def custom_auth():
24
+ # Verify the user's identity with custom logic.
25
+ token = create_jwt(cl.User(identifier="Test User"))
26
+ return JSONResponse({"token": token})
27
+
28
+ @cl.on_chat_start
29
+ async def on_chat_start():
30
+ cl.user_session.set(
31
+ "message_history",
32
+ [{"role": "system", "content": "You are a helpful assistant."}],
33
+ )
34
+ await cl.Message(content="Connected to Chainlit!").send()
35
+
36
+
37
+ @cl.on_message
38
+ async def on_message(message: cl.Message):
39
+ message_history = cl.user_session.get("message_history")
40
+ message_history.append({"role": "user", "content": message.content})
41
+
42
+ msg = cl.Message(content="")
43
+ await msg.send()
44
+
45
+ stream = await client.chat.completions.create(
46
+ messages=message_history, stream=True, **settings
47
+ )
48
+
49
+ async for part in stream:
50
+ if token := part.choices[0].delta.content or "":
51
+ await msg.stream_token(token)
52
+
53
+ message_history.append({"role": "assistant", "content": msg.content})
54
  await msg.update()