c111 / llm_test.py
Shengqian12138's picture
Add files using upload-large-folder tool
d93a2aa verified
Raw
History Blame Contribute Delete
2.81 kB
import json
from openai import OpenAI
import requests
import os
import time
from multiprocessing import Pool, Manager
import threading
import re
import random
# ================= 配置区域 =================
API_KEY = "token-abc123"
BASE_URL = "http://localhost:1054/v1"
MODEL_NAME = "/mnt/tidal-alsh01/usr/dawo/dawo/model_ori/Qwen/Qwen2.5-72B-Instruct"
def get_response(q):
client = OpenAI(api_key=API_KEY, base_url=BASE_URL)
response = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "user", "content": q}
],
temperature=0.7,
)
return response.choices[0].message.content
class ALLIN:
def __init__(self):
# 使用原参考代码2的 API Key
self.api_key = "9b6dce6b66eb42f49aa74635e5484de9"
self.url = "https://runway.devops.rednote.life/openai/google/v1:generateContent"
self.headers = {
"api-key": self.api_key,
"Content-Type": "application/json"
}
def get_response(self, messages, max_tokens=8192, temperature=0.6):
"""
调用Gemini API获取响应
"""
try:
# 提取 system 和 user 消息
system_text = None
user_text = None
for m in messages:
if m["role"] == "system":
system_text = m["content"]
elif m["role"] == "user":
user_text = m["content"]
# 构建请求体
data = {
"contents": [
{
"role": "user",
"parts": [{"text": user_text or ""}]
}
],
"generationConfig": {
"temperature": temperature,
"maxOutputTokens": max_tokens,
"topP": 1,
"seed": 0
# ,
# "thinkingConfig": {
# "includeThoughts": True
# }
}
}
if system_text:
data["systemInstruction"] = {
"parts": [{"text": system_text}]
}
response = requests.post(self.url, headers=self.headers, json=data, timeout=180)
res_dict = response.json()
# 解析返回的文本
return res_dict["candidates"][0]["content"]["parts"][0]["text"]
except Exception as e:
print(f"Gemini 调用失败: {str(e)}", flush=True)
return None
llm = ALLIN()
messages = [
{
"role": "system",
"content": ""
},
{
"role": "user",
"content": "你好,现在是什么时间"
}
]
print(llm.get_response(messages))