Spaces:
Runtime error
Runtime error
Update PCAgent/api.py
Browse files- PCAgent/api.py +31 -14
PCAgent/api.py
CHANGED
|
@@ -35,32 +35,49 @@ def inference_chat(chat, model, api_url, token):
|
|
| 35 |
for role, content in chat:
|
| 36 |
messages.append({"role": role, "content": content})
|
| 37 |
|
| 38 |
-
client = OpenAI(
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
num_try = 5
|
| 46 |
for _ in range(num_try):
|
| 47 |
try:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
messages=messages
|
| 51 |
-
)
|
| 52 |
except:
|
| 53 |
print("Network Error:")
|
| 54 |
try:
|
| 55 |
-
print(
|
| 56 |
except:
|
| 57 |
print("Request Failed")
|
| 58 |
time.sleep(2)
|
| 59 |
else:
|
| 60 |
break
|
| 61 |
-
|
| 62 |
|
| 63 |
-
return
|
|
|
|
| 64 |
|
| 65 |
# headers = {
|
| 66 |
# "Content-Type": "application/json",
|
|
|
|
| 35 |
for role, content in chat:
|
| 36 |
messages.append({"role": role, "content": content})
|
| 37 |
|
| 38 |
+
# client = OpenAI(
|
| 39 |
+
# # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
|
| 40 |
+
# api_key=token,
|
| 41 |
+
# base_url=api_url,
|
| 42 |
+
# )
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# num_try = 5
|
| 46 |
+
# for _ in range(num_try):
|
| 47 |
+
# try:
|
| 48 |
+
# completion = client.chat.completions.create(
|
| 49 |
+
# model=model, # 此处以qwen-plus为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
|
| 50 |
+
# messages=messages
|
| 51 |
+
# )
|
| 52 |
+
# except:
|
| 53 |
+
# print("Network Error:")
|
| 54 |
+
# try:
|
| 55 |
+
# print(completion.model_dump_json())
|
| 56 |
+
# except:
|
| 57 |
+
# print("Request Failed")
|
| 58 |
+
# time.sleep(2)
|
| 59 |
+
# else:
|
| 60 |
+
# break
|
| 61 |
+
|
| 62 |
+
dashscope.api_key = token
|
| 63 |
+
dashscope.base_http_api_url = api_url
|
| 64 |
num_try = 5
|
| 65 |
for _ in range(num_try):
|
| 66 |
try:
|
| 67 |
+
response = MultiModalConversation.call(model=model, messages=messages)
|
| 68 |
+
response = response['output']['choices'][0]['message']['content'][0]["text"]
|
|
|
|
|
|
|
| 69 |
except:
|
| 70 |
print("Network Error:")
|
| 71 |
try:
|
| 72 |
+
print(response.json())
|
| 73 |
except:
|
| 74 |
print("Request Failed")
|
| 75 |
time.sleep(2)
|
| 76 |
else:
|
| 77 |
break
|
|
|
|
| 78 |
|
| 79 |
+
return response
|
| 80 |
+
# return json.loads(completion.model_dump_json())['choices'][0]['message']['content']
|
| 81 |
|
| 82 |
# headers = {
|
| 83 |
# "Content-Type": "application/json",
|