makhdoomnaeem commited on
Commit
e8b6274
·
verified ·
1 Parent(s): c39358b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -3
app.py CHANGED
@@ -40,14 +40,22 @@ def create_vector_db(text):
40
 
41
  # Function to query Groq API
42
  def query_groq_api(query, context, model="llama-3.3-70b-versatile"):
43
- #url = "https://api.groq.com/openai/v1/chat/completions"
44
  GROQ_API_KEY = "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"
 
 
45
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
46
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
 
 
 
47
  headers = {
48
  "Content-Type": "application/json",
49
- "Authorization": f"Bearer {os.getenv('GROQ_API_KEY')}",
50
  }
 
 
51
  data = {
52
  "model": model,
53
  "messages": [
@@ -57,11 +65,18 @@ def query_groq_api(query, context, model="llama-3.3-70b-versatile"):
57
  }
58
 
59
  try:
 
60
  response = requests.post(url, headers=headers, json=data)
61
  response.raise_for_status() # Raise an error for bad responses
 
 
62
  result = response.json()
 
 
63
  return result.get("choices", [{}])[0].get("message", {}).get("content", "No response.")
 
64
  except requests.exceptions.RequestException as e:
 
65
  return f"Error: {e}"
66
 
67
  # Streamlit App
 
40
 
41
  # Function to query Groq API
42
  def query_groq_api(query, context, model="llama-3.3-70b-versatile"):
43
+ # Define the Groq API key
44
  GROQ_API_KEY = "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"
45
+
46
+ # Optionally set it as an environment variable (not necessary in this case)
47
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
48
+
49
+ # API endpoint (Uncomment the URL)
50
+ url = "https://api.groq.com/openai/v1/chat/completions"
51
+
52
+ # Headers for the API request
53
  headers = {
54
  "Content-Type": "application/json",
55
+ "Authorization": f"Bearer {os.getenv('GROQ_API_KEY')}", # Retrieve from environment
56
  }
57
+
58
+ # Data to send to the API
59
  data = {
60
  "model": model,
61
  "messages": [
 
65
  }
66
 
67
  try:
68
+ # Send POST request to Groq API
69
  response = requests.post(url, headers=headers, json=data)
70
  response.raise_for_status() # Raise an error for bad responses
71
+
72
+ # Get the API response content
73
  result = response.json()
74
+
75
+ # Extract the answer from the response
76
  return result.get("choices", [{}])[0].get("message", {}).get("content", "No response.")
77
+
78
  except requests.exceptions.RequestException as e:
79
+ # Handle errors
80
  return f"Error: {e}"
81
 
82
  # Streamlit App