-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoe-python-example.py
More file actions
147 lines (122 loc) Β· 4.19 KB
/
Copy pathpoe-python-example.py
File metadata and controls
147 lines (122 loc) Β· 4.19 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
"""
ALAIn Poe Integration Examples using different approaches
"""
import os
import asyncio
from typing import List, Dict, Any
# Example 1: Using fastapi-poe (Python SDK) - RECOMMENDED for Python
def poe_sdk_example():
"""Using the official fastapi-poe SDK"""
import fastapi_poe as fp
api_key = os.getenv("POE_API_KEY")
if not api_key:
raise ValueError("POE_API_KEY environment variable not set")
# Create a message
message = fp.ProtocolMessage(role="user", content="Hello! Can you explain what Poe is?")
print("π Getting response from Poe using Python SDK...")
try:
for partial in fp.get_bot_response_sync(
messages=[message],
bot_name="GPT-4o", # or "Claude-3.5-Sonnet", "Gemini-1.5-Pro", etc.
api_key=api_key
):
print(partial, end="", flush=True)
print("\nβ
Success!")
except Exception as e:
print(f"β Error: {e}")
# Example 2: Using OpenAI SDK with Poe endpoint
def poe_openai_sdk_example():
"""Using OpenAI SDK configured for Poe API"""
from openai import OpenAI
api_key = os.getenv("POE_API_KEY")
if not api_key:
raise ValueError("POE_API_KEY environment variable not set")
# Configure OpenAI client for Poe
client = OpenAI(
api_key=api_key,
base_url="https://api.poe.com/v1" # Poe's OpenAI-compatible endpoint
)
print("π Getting response from Poe using OpenAI SDK...")
try:
response = client.chat.completions.create(
model="GPT-4o", # Poe model names
messages=[
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": "Explain the difference between GPT and Claude models."}
],
temperature=0.7,
max_tokens=500,
stream=False
)
print(response.choices[0].message.content)
print("β
Success!")
except Exception as e:
print(f"β Error: {e}")
# Example 3: cURL approach (for reference/testing)
def curl_example():
"""cURL command for testing Poe API"""
api_key = os.getenv("POE_API_KEY")
if not api_key:
print("β POE_API_KEY environment variable not set")
return
curl_command = f'''
curl -X POST "https://api.poe.com/v1/chat/completions" \\
-H "Authorization: Bearer {api_key}" \\
-H "Content-Type: application/json" \\
-d '{{
"model": "GPT-4o",
"messages": [
{{
"role": "user",
"content": "Hello from ALAIn platform!"
}}
],
"temperature": 0.7,
"max_tokens": 150
}}'
'''
print("π cURL command for testing:")
print(curl_command)
print("\nπ‘ Run this in your terminal to test the Poe API directly")
# Example 4: Streaming with fastapi-poe
async def poe_streaming_example():
"""Streaming example using fastapi-poe"""
import fastapi_poe as fp
api_key = os.getenv("POE_API_KEY")
if not api_key:
raise ValueError("POE_API_KEY environment variable not set")
message = fp.ProtocolMessage(
role="user",
content="Write a short poem about AI learning platforms."
)
print("π Streaming response from Poe...")
try:
async for partial in fp.get_bot_response(
messages=[message],
bot_name="Claude-3.5-Sonnet",
api_key=api_key
):
print(partial, end="", flush=True)
print("\nβ
Streaming complete!")
except Exception as e:
print(f"β Error: {e}")
def main():
"""Run all examples"""
print("π€ ALAIn Poe Integration Examples")
print("=" * 50)
# Check if API key is available
if not os.getenv("POE_API_KEY"):
print("β οΈ Please set POE_API_KEY environment variable first!")
print(" Get your key from: https://poe.com/api_key")
return
print("\n1οΈβ£ Testing fastapi-poe SDK:")
poe_sdk_example()
print("\n2οΈβ£ Testing OpenAI SDK with Poe:")
poe_openai_sdk_example()
print("\n3οΈβ£ cURL command:")
curl_example()
print("\n4οΈβ£ Testing streaming with fastapi-poe:")
asyncio.run(poe_streaming_example())
if __name__ == "__main__":
main()