-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_example.py
More file actions
90 lines (74 loc) · 2.48 KB
/
Copy pathpython_example.py
File metadata and controls
90 lines (74 loc) · 2.48 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
"""
蓝鹰AI网关 - Python调用示例
BlueEagle AI Gateway - Python Usage Example
官方文档: https://ahg.codes
"""
from openai import OpenAI
# 初始化客户端 | Initialize client
client = OpenAI(
base_url="https://ahg.codes/v1",
api_key="YOUR_API_KEY" # 从控制台获取 | Get from dashboard
)
def chat_with_gpt4o():
"""使用 GPT-4o 进行对话 | Chat with GPT-4o"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, BlueEagle!"}
],
temperature=0.7,
max_tokens=2048
)
print("GPT-4o Response:")
print(response.choices[0].message.content)
def chat_with_claude():
"""使用 Claude 3.5 Sonnet 进行对话 | Chat with Claude 3.5 Sonnet"""
response = client.chat.completions.create(
model="claude-3-5-sonnet-20241022",
messages=[
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
temperature=0.5,
max_tokens=4096
)
print("Claude 3.5 Sonnet Response:")
print(response.choices[0].message.content)
def chat_with_gemini():
"""使用 Gemini 1.5 Pro 进行对话 | Chat with Gemini 1.5 Pro"""
response = client.chat.completions.create(
model="gemini-1.5-pro-latest",
messages=[
{"role": "user", "content": "Write a Python function to calculate Fibonacci numbers."}
],
temperature=0.3,
max_tokens=2048
)
print("Gemini 1.5 Pro Response:")
print(response.choices[0].message.content)
def stream_chat():
"""流式输出示例 | Streaming response example"""
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Tell me a short story."}],
stream=True,
max_tokens=1024
)
print("Streaming Response:")
for chunk in response:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print()
def list_models():
"""列出可用模型 | List available models"""
models = client.models.list()
print("Available Models:")
for model in models.data:
print(f" - {model.id}")
if __name__ == "__main__":
# 选择要运行的示例 | Choose example to run
chat_with_gpt4o()
# chat_with_claude()
# chat_with_gemini()
# stream_chat()
# list_models()