leonsimon23 commited on
Commit
a313d63
·
verified ·
1 Parent(s): 7a40433

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -13
app.py CHANGED
@@ -59,7 +59,7 @@ def chat_with_fastgpt(message, history):
59
  "detail": False,
60
  "messages": messages,
61
  }
62
-
63
  try:
64
  response = requests.post(
65
  f"{BASE_URL}/chat/completions", headers=headers, json=data, stream=True
@@ -72,20 +72,12 @@ def chat_with_fastgpt(message, history):
72
  if chunk:
73
  full_response += chunk.decode('utf-8')
74
 
 
 
 
75
  # 2. Parse the response as a whole
76
  try:
77
- # 尝试解析整个响应为一个 JSON 对象
78
- json_data = json.loads(full_response)
79
-
80
- # 3. Yield in chunks (assuming a simple structure for now)
81
- if json_data.get("choices"):
82
- for choice in json_data["choices"]:
83
- if choice.get("message") and choice["message"].get("content"):
84
- content = choice["message"]["content"]
85
- for i in range(0, len(content), 10): # Yield chunks of 10 characters
86
- yield content[i:i+10]
87
- else:
88
- yield "Invalid response format: 'choices' key not found."
89
 
90
  except json.JSONDecodeError:
91
  print(f"Failed to parse JSON: {full_response}")
 
59
  "detail": False,
60
  "messages": messages,
61
  }
62
+ ####
63
  try:
64
  response = requests.post(
65
  f"{BASE_URL}/chat/completions", headers=headers, json=data, stream=True
 
72
  if chunk:
73
  full_response += chunk.decode('utf-8')
74
 
75
+ # Print the raw response for debugging
76
+ print(f"------ Raw API Response ------\n{full_response}\n-----------------------------")
77
+
78
  # 2. Parse the response as a whole
79
  try:
80
+ # ... (rest of the parsing and yielding logic)
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  except json.JSONDecodeError:
83
  print(f"Failed to parse JSON: {full_response}")