22:基于 Python 的 AI 对话接口调用项目

2323

Branch1Tags0

import os import requests import json

API_URL = "https://api-ai.gitcode.com/v1/chat/completions" headers = { "Authorization": f"Bearer Rmz5QxKKPsmyb6S7LpJ2NBG6", }

def query(payload): response = requests.post(API_URL, headers=headers, json=payload, stream=True) for line in response.iter_lines(): if not line.startswith(b"data:"): continue if line.strip() == b"data:[DONE]": return yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n"))

chunks = query({ "model": "deepseek-v3.1", "messages": [ { "role": "user", "content": "告诉我一个有关宇宙的有趣事实?" } ], "stream": True, "max_tokens": 4096, "temperature": 0.6, "top_p": 0.95, "top_k": 50, "frequency_penalty": 0, "thinking_budget": 32768 })

for chunk in chunks: print(chunk["choices"][0]["delta"]["content"], end="") import os import requests import json

API_URL = "https://api-ai.gitcode.com/v1/chat/completions" headers = { "Authorization": f"Bearer Rmz5QxKKPsmyb6S7LpJ2NBG6", }

def query(payload): response = requests.post(API_URL, headers=headers, json=payload, stream=True) for line in response.iter_lines(): if not line.startswith(b"data:"): continue if line.strip() == b"data:[DONE]": return yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n"))

chunks = query({ "model": "deepseek-v3.1", "messages": [ { "role": "user", "content": "告诉我一个有关宇宙的有趣事实?" } ], "stream": True, "max_tokens": 4096, "temperature": 0.6, "top_p": 0.95, "top_k": 50, "frequency_penalty": 0, "thinking_budget": 32768 })

for chunk in chunks: print(chunk["choices"][0]["delta"]["content"], end="")