def call_deepseek_api(messages):
"""
Calls the DeepSeek API with the given messages.
Args:
messages: A list of dictionaries, where each dictionary represents a message.
Each message dictionary should have "role" and "content" keys.
Returns:
A dictionary containing the API response, or None if an error occurs.
"""
url = "http://api.bingapi.us.kg:2095/v1/chat/completions"
headers = {
"Authorization": "Bearer op_Fu0vdY7Gjko69IkYlg69Fr",
"Content-Type": "application/json"
}
data = {
"model": "deepseek-r1",
"messages": messages
}
try:
response = requests.post(url, headers=headers, data=json.dumps(data), timeout=300)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error calling DeepSeek API: {e}")
return None
# Example usage:
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "写一个汽车广告"}
]
response = call_deepseek_api(messages)
if response:
print(json.dumps(response, indent=2)) # Print the response nicely formatted
try:
assistant_message = response["choices"][0]["message"]["content"]
print("\nAssistant:", assistant_message) # Extract and print the assistant's message.
except (KeyError, IndexError) as e:
print(f"Error extracting assistant's message from response: {e}. Full response:\n{response}")