Niansuh commited on
Commit
4279de7
·
verified ·
1 Parent(s): b4b4823

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -4
app.py CHANGED
@@ -34,13 +34,42 @@ data = {
34
  'model': 'solar-pro'
35
  }
36
 
37
- # Send the POST request
38
  response = requests.post(url, headers=headers, json=data)
39
 
40
- # Check the status of the response and print the result
41
  if response.status_code == 200:
42
- print('Response received successfully:')
43
- print(json.dumps(response.json(), indent=2))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  else:
 
45
  print(f'Failed to get a response. Status code: {response.status_code}')
46
  print(response.text)
 
34
  'model': 'solar-pro'
35
  }
36
 
37
+ # Send the POST request to the API
38
  response = requests.post(url, headers=headers, json=data)
39
 
40
+ # Check if the request was successful
41
  if response.status_code == 200:
42
+ # Parse the response JSON
43
+ api_response = response.json()
44
+
45
+ # Assuming the API response structure contains 'message' field under 'choices'
46
+ # Convert the response to OpenAI format
47
+ openai_response = {
48
+ "id": api_response.get("id", "unknown_id"), # You can use 'id' or generate your own
49
+ "object": "chat.completion",
50
+ "created": api_response.get("created", 0), # Timestamp if available
51
+ "model": data['model'], # The model you used
52
+ "usage": {
53
+ "prompt_tokens": api_response.get("prompt_tokens", 0), # This might be in your API response
54
+ "completion_tokens": api_response.get("completion_tokens", 0), # This too
55
+ "total_tokens": api_response.get("total_tokens", 0)
56
+ },
57
+ "choices": [
58
+ {
59
+ "message": {
60
+ "role": api_response.get("messages", [{}])[0].get('role', 'user'), # Role from the response
61
+ "content": api_response.get("messages", [{}])[0].get('content', 'No response content')
62
+ },
63
+ "finish_reason": "stop", # This can be dynamic based on the response
64
+ "index": 0 # Default for single response
65
+ }
66
+ ]
67
+ }
68
+
69
+ # Print the OpenAI formatted response
70
+ print(json.dumps(openai_response, indent=2))
71
+
72
  else:
73
+ # If the request fails, print the error message
74
  print(f'Failed to get a response. Status code: {response.status_code}')
75
  print(response.text)