| 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): |
| |
| 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_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 |
| |
| |
| |
| |
| } |
| } |
|
|
| 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)) |
|
|