Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,27 @@
|
|
|
|
1 |
import chainlit as cl
|
2 |
from together import Together
|
3 |
|
4 |
-
# Hardcoded Together API key for testing (replace with your key)
|
5 |
TOGETHER_API_KEY = "4e381cebe7224f3da54cae6e54d3fdd3ee00b6ac160fc29cea66fbe48a0be3d1"
|
6 |
|
7 |
@cl.on_message
|
8 |
async def main(message: str):
|
9 |
-
|
10 |
-
|
11 |
-
if not together_api_key:
|
12 |
-
await cl.Message(
|
13 |
-
content="Error: No Together API key provided."
|
14 |
-
).send()
|
15 |
return
|
16 |
|
17 |
-
|
18 |
-
client = Together(api_key=together_api_key)
|
19 |
-
|
20 |
-
# Construct messages from the user input
|
21 |
messages = [{"role": "user", "content": message}]
|
22 |
|
23 |
try:
|
24 |
-
#
|
25 |
-
completion =
|
|
|
26 |
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
27 |
messages=messages,
|
28 |
max_tokens=500
|
29 |
)
|
30 |
-
# Extract the response
|
31 |
response = completion.choices[0].message.content
|
32 |
-
# Send the response via Chainlit
|
33 |
await cl.Message(content=response).send()
|
34 |
except Exception as e:
|
35 |
-
# If there is an error, send the error message
|
36 |
await cl.Message(content=f"Error: {str(e)}").send()
|
|
|
1 |
+
import asyncio
|
2 |
import chainlit as cl
|
3 |
from together import Together
|
4 |
|
|
|
5 |
TOGETHER_API_KEY = "4e381cebe7224f3da54cae6e54d3fdd3ee00b6ac160fc29cea66fbe48a0be3d1"
|
6 |
|
7 |
@cl.on_message
|
8 |
async def main(message: str):
|
9 |
+
if not TOGETHER_API_KEY:
|
10 |
+
await cl.Message(content="Error: No Together API key provided.").send()
|
|
|
|
|
|
|
|
|
11 |
return
|
12 |
|
13 |
+
client = Together(api_key=TOGETHER_API_KEY)
|
|
|
|
|
|
|
14 |
messages = [{"role": "user", "content": message}]
|
15 |
|
16 |
try:
|
17 |
+
# Run the synchronous API call in a thread
|
18 |
+
completion = await asyncio.to_thread(
|
19 |
+
client.chat.completions.create,
|
20 |
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
21 |
messages=messages,
|
22 |
max_tokens=500
|
23 |
)
|
|
|
24 |
response = completion.choices[0].message.content
|
|
|
25 |
await cl.Message(content=response).send()
|
26 |
except Exception as e:
|
|
|
27 |
await cl.Message(content=f"Error: {str(e)}").send()
|