delightfulrachel commited on
Commit
c14aecf
·
verified ·
1 Parent(s): d0ee96d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import os
3
  import time
4
  import json
5
- from together import Together
6
 
7
  # Set environment variables
8
  # Note: For Hugging Face Spaces, you'll set these in the Secrets tab
@@ -21,11 +21,11 @@ def get_together_client():
21
  api_key = os.environ.get("TOGETHER_API_KEY")
22
  if not api_key:
23
  raise ValueError("TOGETHER_API_KEY not found. Please set it in the environment variables.")
24
- return Together(api_key=api_key)
25
 
26
- # Function to call LLM
27
  def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
28
- client = get_together_client()
29
 
30
  # System message for Salesforce expertise
31
  system_message = "You are a Salesforce development expert specializing in B2B Commerce migrations, CloudCraze to B2B Lightning Experience conversions, and Apex code optimization. Provide clear, step-by-step guidance and corrected code."
@@ -33,18 +33,33 @@ def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
33
  start_time = time.time()
34
 
35
  try:
36
- response = client.chat.completions.create(
37
- model=model,
38
- messages=[
 
 
 
 
 
39
  {"role": "system", "content": system_message},
40
  {"role": "user", "content": prompt}
41
  ],
42
- temperature=temperature,
43
- max_tokens=max_tokens,
44
- top_p=0.9
 
 
 
 
 
 
45
  )
46
 
47
- completion_text = response.choices[0].message.content
 
 
 
 
48
 
49
  end_time = time.time()
50
  duration = end_time - start_time
 
2
  import os
3
  import time
4
  import json
5
+ import requests
6
 
7
  # Set environment variables
8
  # Note: For Hugging Face Spaces, you'll set these in the Secrets tab
 
21
  api_key = os.environ.get("TOGETHER_API_KEY")
22
  if not api_key:
23
  raise ValueError("TOGETHER_API_KEY not found. Please set it in the environment variables.")
24
+ return api_key
25
 
26
+ # Function to call LLM using REST API directly
27
  def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
28
+ api_key = get_together_client()
29
 
30
  # System message for Salesforce expertise
31
  system_message = "You are a Salesforce development expert specializing in B2B Commerce migrations, CloudCraze to B2B Lightning Experience conversions, and Apex code optimization. Provide clear, step-by-step guidance and corrected code."
 
33
  start_time = time.time()
34
 
35
  try:
36
+ headers = {
37
+ "Authorization": f"Bearer {api_key}",
38
+ "Content-Type": "application/json"
39
+ }
40
+
41
+ data = {
42
+ "model": model,
43
+ "messages": [
44
  {"role": "system", "content": system_message},
45
  {"role": "user", "content": prompt}
46
  ],
47
+ "temperature": temperature,
48
+ "max_tokens": max_tokens,
49
+ "top_p": 0.9
50
+ }
51
+
52
+ response = requests.post(
53
+ "https://api.together.xyz/v1/chat/completions",
54
+ headers=headers,
55
+ json=data
56
  )
57
 
58
+ if response.status_code != 200:
59
+ return f"Error: API returned status code {response.status_code}: {response.text}"
60
+
61
+ response_data = response.json()
62
+ completion_text = response_data["choices"][0]["message"]["content"]
63
 
64
  end_time = time.time()
65
  duration = end_time - start_time