-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathllm.py
More file actions
59 lines (48 loc) · 1.64 KB
/
Copy pathllm.py
File metadata and controls
59 lines (48 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
from openai import OpenAI
from dotenv import load_dotenv
# 加载环境变量
def load_environment():
load_dotenv()
if not os.getenv("OPENAI_API_KEY"):
raise ValueError("OPENAI_API_KEY not found in environment variables")
# 初始化OpenAI客户端
def init_openai_client():
return OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# 调用模型的通用接口
def call_model(prompt, model_name="gpt-3.5-turbo", temperature=0.7, max_tokens=1000):
"""
调用指定的LLM模型生成回答
参数:
prompt (str): 输入提示
model_name (str): 模型名称
temperature (float): 温度参数
max_tokens (int): 最大生成token数
返回:
str: 模型生成的回答
"""
try:
# 确保环境已加载
load_environment()
# 初始化客户端
client = init_openai_client()
# 调用模型
response = client.chat.completions.create(
model=model_name,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
temperature=temperature,
max_tokens=max_tokens
)
return response.choices[0].message.content
except Exception as e:
print(f"Error calling model {model_name}: {e}")
return None
# 轻量级LLM调用
def lite_llm_infer(prompt):
return call_model(prompt, model_name="gpt-3.5-turbo", temperature=0.5, max_tokens=500)
# 重量级LLM调用
def heavy_llm_infer(prompt):
return call_model(prompt, model_name="gpt-4", temperature=0.7, max_tokens=2000)