-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathprovider_siliconflow.py
More file actions
51 lines (44 loc) · 1.48 KB
/
provider_siliconflow.py
File metadata and controls
51 lines (44 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
import os
URL = "https://api.siliconflow.cn/v1/chat/completions"
MAX_TOKENS = 4096
headers = {
"Authorization": f"Bearer {os.environ['SILICONFLOW_API_KEY']}",
"Content-Type": "application/json"
}
def call_siliconflow_chat(prompt, system=None, format="text", model="deepseek-ai/DeepSeek-V2.5"):
payload = {
"model": model,
"messages": [],
"stream": False,
"max_tokens": MAX_TOKENS,
"stop": ["null"],
"temperature": 0.7,
"top_p": 0.7,
"top_k": 50,
"frequency_penalty": 0.5,
"n": 1,
"response_format": {"type": format},
}
if system is not None:
payload["messages"].append({
"role": "system",
"content": system
})
payload["messages"].append({
"role": "user",
"content": prompt
})
try:
response = requests.request("POST", URL, json=payload, headers=headers)
#print(response.text)
return response.json()["choices"][0]["message"]["content"]
except Exception as e:
print(e)
return None
# env $(grep -v '^#' .env | xargs) python3.8 provider_siliconflow.py
if __name__ == "__main__":
print(os.environ['SILICONFLOW_API_KEY'])
#response = call_siliconflow_chat("天空为什么是蓝色的")
response = call_siliconflow_chat("天空为什么是蓝色的,output JSON", system="You are a helpful assistant designed to output JSON.", format="json_object")
print(response)