MCPcopy Create free account
hub / github.com/Alibaba-NLP/DeepResearch / call_dashscope

Function call_dashscope

WebAgent/WebWeaver/dashscope_api.py:9–60  ·  view source on GitHub ↗
(model, messages, stop, temperature, top_p, max_tokens, max_retries=10)

Source from the content-addressed store, hash-verified

7
8
9def call_dashscope(model, messages, stop, temperature, top_p, max_tokens, max_retries=10):
10 body = {
11 "messages": [
12 {
13 "role": "user",
14 "content": "Hello!"
15 }
16 ],
17 "extendParams":{
18 "thinkingConfig": {
19 "includeThoughts": True,
20 # "thinkingBudget": 16000
21 }
22 },
23 "model": "o4-mini-2025-04-16", ####gemini-2.5-pro, gpt-4o-2024-11-20, o4-mini-2025-04-16,
24 "max_tokens": max_tokens,
25 }
26
27 url = os.getenv("DASHSCOPE_API_BASE", "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions")
28 api_key = os.getenv("DASHSCOPE_API_KEY")
29 headers = {
30 "Authorization": api_key,
31 "Content-Type": "application/json"
32 }
33
34 max_try = 10
35
36 body = copy.deepcopy(body)
37 body["messages"] = messages
38 body["model"] = model
39 body["max_tokens"] = max_tokens
40 body["stop"] = stop
41 body["temperature"] = temperature
42 body["top_p"] = top_p
43
44 for i in range(max_try):
45 try:
46 r = requests.post(url, json.dumps(body, ensure_ascii=False).encode('utf-8'),
47 headers=headers, timeout=6000)
48
49 response = json.loads(r.text)
50
51 if response["choices"][0]["message"]["content"] is None:
52 print(f"Response is None, retrying {i+1}/{max_try}")
53 time.sleep(random.randint(1, 15) * 0.23)
54 continue
55 return response["choices"][0]["message"]["content"]
56
57 except Exception as e:
58 print(f"Request failed: {e}")
59 time.sleep(random.randint(1, 15) * 0.23)
60 continue
61
62if __name__ == '__main__':
63 response = call_dashscope("qwen3-235b-a22b-instruct-2507", messages=[

Callers 3

_runMethod · 0.90
_runMethod · 0.90
dashscope_api.pyFile · 0.85

Calls 1

encodeMethod · 0.80

Tested by

no test coverage detected