-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloAgentsLLM.py
More file actions
82 lines (76 loc) · 2.65 KB
/
Copy pathHelloAgentsLLM.py
File metadata and controls
82 lines (76 loc) · 2.65 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
from openai import OpenAI
from dotenv import load_dotenv
from typing import List, Dict
# 加载 .env 文件中的环境变量
load_dotenv()
class HelloAgentsLLM:
"""
为 "Hello Agents" 定制的LLM客户端。
它用于调用任何兼容OpenAI接口的服务,并默认使用流式响应。
"""
def __init__(
self,
model: str = None,
apiKey: str = None,
baseUrl: str = None,
timeout: int = None,
):
"""
初始化客户端。优先使用传入参数,如果未提供,则从环境变量加载。
"""
self.model = model or os.getenv("MODEL_NAME")
apiKey = apiKey or os.getenv("DS_API_KEY")
baseUrl = baseUrl or os.getenv("DS_BASE_URL")
timeout = timeout or int(os.getenv("DS_TIMEOUT", 60))
if not all([self.model, apiKey, baseUrl]):
raise ValueError(
"模型ID、API密钥和服务地址必须被提供或在.env文件中定义。"
)
self.client = OpenAI(api_key=apiKey, base_url=baseUrl, timeout=timeout)
def think(
self,
messages: List[Dict[str, str]],
temperature: float = 0,
) -> str:
"""
调用大语言模型进行思考,并返回其响应。
"""
print(f"🧠 正在调用 {self.model} 模型...")
try:
response = self.client.chat.completions.create(
model=self.model,
messages=messages,
temperature=temperature,
stream=True,
)
# 处理流式响应
print("✅ 大语言模型响应成功:")
collected_content = []
for chunk in response:
content = chunk.choices[0].delta.content or ""
print(content, end="", flush=True)
collected_content.append(content)
print() # 在流式输出结束后换行
return "".join(collected_content)
except Exception as e:
print(f"❌ 调用LLM API时发生错误: {e}")
return None
# --- 客户端使用示例 ---
if __name__ == '__main__':
try:
llmClient = HelloAgentsLLM()
exampleMessages = [
{
"role": "system",
"content": "You are a helpful assistant that writes\nPython code."
},
{"role": "user", "content": "你是谁"}
]
print("--- 调用LLM ---")
responseText = llmClient.think(exampleMessages)
if responseText:
print("\n\n--- 完整模型响应 ---")
print(responseText)
except ValueError as e:
print(e)