-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_example.py
More file actions
127 lines (111 loc) · 3.61 KB
/
Copy pathpython_example.py
File metadata and controls
127 lines (111 loc) · 3.61 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
BlueEagle AI Gateway - Python 调用示例
官方文档: https://ahg.codes
"""
from openai import OpenAI
# 初始化客户端 | Initialize client
# 仅需修改 base_url,其余与官方 OpenAI SDK 完全一致
client = OpenAI(
api_key="YOUR_API_KEY", # 替换为您的蓝鹰API密钥
base_url="https://ahg.codes/v1"
)
def chat_non_streaming():
"""非流式对话示例 | Non-streaming chat example"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, BlueEagle AI Gateway!"}
],
temperature=0.7,
max_tokens=500
)
print("=" * 50)
print("非流式响应 | Non-streaming Response:")
print("=" * 50)
print(response.choices[0].message.content)
print(f"\n消耗tokens: {response.usage.total_tokens}")
def chat_streaming():
"""流式对话示例 | Streaming chat example"""
stream = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "请用三句话介绍蓝鹰AI网关的优势"}
],
stream=True,
temperature=0.7
)
print("=" * 50)
print("流式响应 | Streaming Response:")
print("=" * 50)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print("\n")
def chat_with_claude():
"""调用 Claude 模型示例 | Claude model example"""
response = client.chat.completions.create(
model="claude-3-5-sonnet-20241022",
messages=[
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
max_tokens=1000
)
print("=" * 50)
print("Claude 响应 | Claude Response:")
print("=" * 50)
print(response.choices[0].message.content)
def chat_with_gemini():
"""调用 Gemini 模型示例 | Gemini model example"""
response = client.chat.completions.create(
model="gemini-1.5-pro-latest",
messages=[
{"role": "user", "content": "What are the benefits of AI gateways?"}
],
max_tokens=1000
)
print("=" * 50)
print("Gemini 响应 | Gemini Response:")
print("=" * 50)
print(response.choices[0].message.content)
def function_calling_example():
"""函数调用示例 | Function calling example"""
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name, e.g. Beijing"
}
},
"required": ["location"]
}
}
}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "What's the weather like in Shanghai?"}
],
tools=tools,
tool_choice="auto"
)
print("=" * 50)
print("函数调用 | Function Calling:")
print("=" * 50)
print(response.choices[0].message)
if __name__ == "__main__":
print("🦅 BlueEagle AI Gateway - Python Examples\n")
# 运行示例(取消注释以测试)
chat_non_streaming()
# chat_streaming()
# chat_with_claude()
# chat_with_gemini()
# function_calling_example()