-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
74 lines (62 loc) · 2.21 KB
/
test_api.py
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
import os
from dotenv import load_dotenv
import dspy
from openai import OpenAI
import sys
# Load environment variables from .env file
load_dotenv()
def test_openai():
"""Test direct OpenAI API connection"""
try:
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
print("❌ OPENAI_API_KEY environment variable not found")
return False
client = OpenAI(api_key=api_key)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say 'OpenAI test successful'"}],
max_tokens=20
)
print("✅ OpenAI API test successful")
print(f"Response: {response.choices[0].message.content}")
return True
except Exception as e:
print(f"❌ OpenAI API test failed: {str(e)}")
return False
def test_dspy():
"""Test DSPy configuration and basic functionality"""
try:
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
print("❌ OPENAI_API_KEY environment variable not found")
return False
# Configure DSPy
dspy.configure(api_key=api_key)
openai_lm = dspy.OpenAI(model="gpt-4", api_key=api_key)
dspy.settings.configure(lm=openai_lm)
# Create a simple DSPy module for testing
class SimpleModule(dspy.Module):
def __init__(self):
super().__init__()
self.generate = dspy.ChainOfThought("input -> output")
def forward(self, input_text):
return self.generate(input=input_text)
# Test the module
module = SimpleModule()
result = module("Say 'DSPy test successful'")
print("✅ DSPy test successful")
print(f"Response: {result.output}")
return True
except Exception as e:
print(f"❌ DSPy test failed: {str(e)}")
return False
def main():
print("\n=== Testing OpenAI API ===")
openai_success = test_openai()
print("\n=== Testing DSPy ===")
dspy_success = test_dspy()
if not (openai_success and dspy_success):
sys.exit(1)
if __name__ == "__main__":
main()