'commit'
This commit is contained in:
@@ -4,14 +4,16 @@ import requests
|
|||||||
|
|
||||||
from Config.Config import GPTNB_API_KEY
|
from Config.Config import GPTNB_API_KEY
|
||||||
|
|
||||||
|
|
||||||
class ModelInteractor:
|
class ModelInteractor:
|
||||||
def __init__(self):
|
def __init__(self, api_key=GPTNB_API_KEY, api_url="https://goapi.gptnb.ai/v1/chat/completions"):
|
||||||
self.api_url = "https://goapi.gptnb.ai/v1/chat/completions",
|
self.api_key = api_key
|
||||||
|
self.api_url = api_url
|
||||||
self.headers = {
|
self.headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Bearer {GPTNB_API_KEY}"
|
"Authorization": f"Bearer {self.api_key}"
|
||||||
}
|
}
|
||||||
|
|
||||||
def stream_request(self, model, prompt, temperature=0.7):
|
def stream_request(self, model, prompt, temperature=0.7):
|
||||||
"""
|
"""
|
||||||
发送流式请求到模型API
|
发送流式请求到模型API
|
||||||
@@ -33,7 +35,7 @@ class ModelInteractor:
|
|||||||
"temperature": temperature,
|
"temperature": temperature,
|
||||||
"stream": True
|
"stream": True
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
self.api_url,
|
self.api_url,
|
||||||
@@ -43,24 +45,24 @@ class ModelInteractor:
|
|||||||
timeout=30
|
timeout=30
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
print(f"使用模型 {model} 的流式响应内容: ")
|
print(f"使用模型 {model} 的流式响应内容: ")
|
||||||
for chunk in response.iter_content(chunk_size=None):
|
for chunk in response.iter_content(chunk_size=None):
|
||||||
if chunk:
|
if chunk:
|
||||||
chunk_data = chunk.decode('utf-8', errors='replace')
|
chunk_data = chunk.decode('utf-8', errors='replace')
|
||||||
|
|
||||||
for line in chunk_data.splitlines():
|
for line in chunk_data.splitlines():
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if line == 'data: [DONE]':
|
if line == 'data: [DONE]':
|
||||||
print("\n流式响应结束")
|
print("\n流式响应结束")
|
||||||
return
|
return
|
||||||
|
|
||||||
if line.startswith('data: '):
|
if line.startswith('data: '):
|
||||||
line = line[6:]
|
line = line[6:]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = json.loads(line)
|
data = json.loads(line)
|
||||||
if 'choices' in data and len(data['choices']) > 0:
|
if 'choices' in data and len(data['choices']) > 0:
|
||||||
@@ -70,18 +72,19 @@ class ModelInteractor:
|
|||||||
print(content, end='', flush=True)
|
print(content, end='', flush=True)
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
print(f"[调试] JSON解析错误: {e}, 内容: {line}")
|
print(f"[调试] JSON解析错误: {e}, 内容: {line}")
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"请求发生错误: {e}")
|
print(f"请求发生错误: {e}")
|
||||||
|
|
||||||
|
|
||||||
# 示例使用
|
# 示例使用
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 创建模型交互器实例
|
# 创建模型交互器实例
|
||||||
interactor = ModelInteractor()
|
interactor = ModelInteractor()
|
||||||
|
|
||||||
# 使用不同的模型和提示词
|
# 使用不同的模型和提示词
|
||||||
model_name = "gemini-2.5-pro"
|
model_name = "gemini-2.5-pro"
|
||||||
prompt_text = "请详细介绍一下你自己,分成几个段落来说明"
|
prompt_text = "请详细介绍一下你自己,分成几个段落来说明"
|
||||||
|
|
||||||
# 发送流式请求
|
# 发送流式请求
|
||||||
interactor.stream_request(model_name, prompt_text)
|
interactor.stream_request(model_name, prompt_text)
|
||||||
|
Reference in New Issue
Block a user