Bhavesh7895 commited on
Commit
a95729f
·
verified ·
1 Parent(s): 89e67c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -4,17 +4,18 @@ 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
  {
20
  "role": "user",
@@ -23,15 +24,19 @@ async def main(message: str):
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()
 
4
 
5
  @cl.on_message
6
  async def main(message: str):
7
+ # Read your Together API key from the 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 in your Space secrets."
12
  ).send()
13
  return
14
 
15
+ # Initialize the Together client
16
  client = Together(api_key=together_api_key)
17
 
18
+ # Construct messages
19
  messages = [
20
  {
21
  "role": "user",
 
24
  ]
25
 
26
  try:
27
+ # Call the model
28
  completion = client.chat.completions.create(
29
  model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
30
  messages=messages,
31
  max_tokens=500
32
  )
33
+
34
+ # Extract the response text
35
  response = completion.choices[0].message.content
36
+
37
+ # Send the response to Chainlit
38
  await cl.Message(content=response).send()
39
+
40
  except Exception as e:
41
+ # Send any error info to Chainlit
42
  await cl.Message(content=f"Error: {str(e)}").send()