Spaces:
Runtime error
Runtime error
bentebbutt
commited on
Commit
β’
829301b
1
Parent(s):
85e98ba
Upload 2 files
Browse files- backend/app.py +54 -0
- backend/chainlit.md +14 -0
backend/app.py
ADDED
@@ -0,0 +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()
|
backend/chainlit.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Welcome to Chainlit! ππ€
|
2 |
+
|
3 |
+
Hi there, Developer! π We're excited to have you on board. Chainlit is a powerful tool designed to help you prototype, debug and share applications built on top of LLMs.
|
4 |
+
|
5 |
+
## Useful Links π
|
6 |
+
|
7 |
+
- **Documentation:** Get started with our comprehensive [Chainlit Documentation](https://docs.chainlit.io) π
|
8 |
+
- **Discord Community:** Join our friendly [Chainlit Discord](https://discord.gg/k73SQ3FyUh) to ask questions, share your projects, and connect with other developers! π¬
|
9 |
+
|
10 |
+
We can't wait to see what you create with Chainlit! Happy coding! π»π
|
11 |
+
|
12 |
+
## Welcome screen
|
13 |
+
|
14 |
+
To modify the welcome screen, edit the `chainlit.md` file at the root of your project. If you do not want a welcome screen, just leave this file empty.
|