-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_example.py
More file actions
111 lines (88 loc) · 3.1 KB
/
Copy pathpython_example.py
File metadata and controls
111 lines (88 loc) · 3.1 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
"""
蓝鹰AI网关 (BlueEagle AI Gateway) - Python调用示例
Official Website: https://ahg.codes
"""
from openai import OpenAI
def chat_completion_example():
"""基础对话示例 | Basic Chat Completion Example"""
client = OpenAI(
api_key="your-api-key-here", # 替换为您的蓝鹰AI网关API密钥
base_url="https://ahg.codes/v1" # 蓝鹰AI网关Base URL
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好!请介绍一下你自己。"}
],
max_tokens=1024,
temperature=0.7
)
print("Response:", response.choices[0].message.content)
print("Tokens used:", response.usage)
return response
def streaming_example():
"""流式响应示例 | Streaming Response Example"""
client = OpenAI(
api_key="your-api-key-here",
base_url="https://ahg.codes/v1"
)
stream = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "请写一首关于AI的短诗。"}
],
stream=True
)
print("Streaming response:")
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print()
def multi_model_example():
"""多模型切换示例 | Multi-Model Switching Example"""
client = OpenAI(
api_key="your-api-key-here",
base_url="https://ahg.codes/v1"
)
models = ["gpt-4o", "claude-4-sonnet", "gemini-2.5-pro"]
for model in models:
print(f"\n--- Testing {model} ---")
try:
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "Say hello in one sentence."}],
max_tokens=100
)
print(f"{model}: {response.choices[0].message.content}")
except Exception as e:
print(f"{model}: Error - {e}")
def embedding_example():
"""Embedding示例 | Embedding Example"""
client = OpenAI(
api_key="your-api-key-here",
base_url="https://ahg.codes/v1"
)
response = client.embeddings.create(
model="text-embedding-3-large",
input="Hello, BlueEagle AI Gateway!"
)
print("Embedding dimensions:", len(response.data[0].embedding))
print("First 5 values:", response.data[0].embedding[:5])
if __name__ == "__main__":
print("=" * 60)
print("🦅 蓝鹰AI网关 (BlueEagle AI Gateway) - Python Examples")
print("🌐 Official Website: https://ahg.codes")
print("=" * 60)
# 运行基础对话示例
print("\n📝 Running basic chat completion...")
chat_completion_example()
# 运行流式响应示例
print("\n📝 Running streaming response...")
streaming_example()
# 运行多模型切换示例
print("\n📝 Running multi-model test...")
multi_model_example()
# 运行Embedding示例
print("\n📝 Running embedding example...")
embedding_example()