Spaces:
Runtime error
Runtime error
Update backend/app.py
Browse files- backend/app.py +18 -35
backend/app.py
CHANGED
|
@@ -8,47 +8,30 @@ from chainlit.server import app
|
|
| 8 |
import chainlit as cl
|
| 9 |
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
| 31 |
-
"
|
| 32 |
-
|
| 33 |
)
|
| 34 |
-
|
| 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 |
-
|
| 43 |
-
await msg.send()
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 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 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import chainlit as cl
|
| 9 |
|
| 10 |
|
| 11 |
+
import chainlit as cl
|
| 12 |
+
import langroid as lr
|
| 13 |
+
from langroid.agent.callbacks.chainlit import add_instructions
|
| 14 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@cl.on_chat_start
|
| 17 |
async def on_chat_start():
|
| 18 |
+
config = lr.ChatAgentConfig(
|
| 19 |
+
name="Demo",
|
| 20 |
+
system_message="You are a helpful assistant. Be concise in your answers.",
|
| 21 |
)
|
| 22 |
+
agent = lr.ChatAgent(config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
cl.user_session.set("agent", agent)
|
|
|
|
| 25 |
|
| 26 |
+
await add_instructions(
|
| 27 |
+
title="Instructions",
|
| 28 |
+
content="Interact with a **Langroid ChatAgent**",
|
| 29 |
)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
@cl.on_message
|
| 33 |
+
async def on_message(message: cl.Message):
|
| 34 |
+
agent: lr.ChatAgent = cl.user_session.get("agent")
|
| 35 |
+
# important: only apply callbacks after getting first msg.
|
| 36 |
+
lr.ChainlitAgentCallbacks(agent, message)
|
| 37 |
+
await agent.llm_response_async(message.content)
|