Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,19 @@
|
|
1 |
import os
|
2 |
import chainlit as cl
|
3 |
-
from
|
4 |
-
|
5 |
-
# Make sure you set HF_API_KEY in your environment or Spaces Secrets.
|
6 |
-
# E.g., os.environ["HF_API_KEY"] = "hf_xxxxxxxxxxxxxx"
|
7 |
|
8 |
@cl.on_message
|
9 |
async def main(message: str):
|
10 |
-
|
11 |
-
|
|
|
12 |
await cl.Message(
|
13 |
-
content="Error: No
|
14 |
).send()
|
15 |
return
|
16 |
|
17 |
-
client
|
|
|
18 |
|
19 |
messages = [
|
20 |
{
|
@@ -24,12 +23,15 @@ async def main(message: str):
|
|
24 |
]
|
25 |
|
26 |
try:
|
|
|
27 |
completion = client.chat.completions.create(
|
28 |
-
model="
|
29 |
messages=messages,
|
30 |
max_tokens=500
|
31 |
)
|
|
|
32 |
response = completion.choices[0].message.content
|
|
|
33 |
await cl.Message(content=response).send()
|
34 |
except Exception as e:
|
35 |
-
await cl.Message(content=f"Error: {str(e)}").send()
|
|
|
1 |
import os
|
2 |
import chainlit as cl
|
3 |
+
from together import Together
|
|
|
|
|
|
|
4 |
|
5 |
@cl.on_message
|
6 |
async def main(message: str):
|
7 |
+
# Read your Together API key from environment
|
8 |
+
together_api_key = os.getenv("TOGETHER_API_KEY", "")
|
9 |
+
if not together_api_key:
|
10 |
await cl.Message(
|
11 |
+
content="Error: No Together API key found. Please set TOGETHER_API_KEY."
|
12 |
).send()
|
13 |
return
|
14 |
|
15 |
+
# Initialize the Together client with your API key
|
16 |
+
client = Together(api_key=together_api_key)
|
17 |
|
18 |
messages = [
|
19 |
{
|
|
|
23 |
]
|
24 |
|
25 |
try:
|
26 |
+
# Call the desired model
|
27 |
completion = client.chat.completions.create(
|
28 |
+
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
29 |
messages=messages,
|
30 |
max_tokens=500
|
31 |
)
|
32 |
+
# Extract response
|
33 |
response = completion.choices[0].message.content
|
34 |
+
# Send Chainlit message
|
35 |
await cl.Message(content=response).send()
|
36 |
except Exception as e:
|
37 |
+
await cl.Message(content=f"Error: {str(e)}").send()
|