MCPcopy Create free account
hub / github.com/X-PLUG/MobileAgent / inference_chat

Function inference_chat

PC-Agent/PCAgent/api.py:32–76  ·  view source on GitHub ↗
(chat, model, api_url, token)

Source from the content-addressed store, hash-verified

30
31
32def inference_chat(chat, model, api_url, token):
33
34 messages = []
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
63 return json.loads(completion.model_dump_json())['choices'][0]['message']['content']
64
65 # headers = {
66 # "Content-Type": "application/json",
67 # "Authorization": f"Bearer {token}"
68 # }
69
70 # data = {
71 # "model": model,
72 # "messages": [],
73 # "max_tokens": 2048,
74 # 'temperature': 0.0,
75 # "seed": 1234
76 # }
77

Callers 3

selectFunction · 0.90
run.pyFile · 0.90
run_v1.pyFile · 0.90

Calls 3

model_dump_jsonMethod · 0.80
createMethod · 0.45
sleepMethod · 0.45

Tested by

no test coverage detected