Bhavesh7895 commited on
Commit
414cee7
·
verified ·
1 Parent(s): a95729f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -17
app.py CHANGED
@@ -1,27 +1,24 @@
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 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",
22
- "content": message
23
- }
24
- ]
25
 
26
  try:
27
  # Call the model
@@ -30,13 +27,10 @@ async def main(message: str):
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()
 
 
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
+ # Use the hardcoded key directly
10
+ together_api_key = TOGETHER_API_KEY
11
  if not together_api_key:
12
  await cl.Message(
13
+ content="Error: No Together API key provided."
14
  ).send()
15
  return
16
 
17
  # Initialize the Together client
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
  # Call the model
 
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()