-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-handler.py
68 lines (62 loc) · 2.33 KB
/
api-handler.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
import openai
import anthropic
import os
from typing import Dict, Any
from .base_handler import BaseAPIHandler
class OpenAIHandler(BaseAPIHandler):
def __init__(self, api_key: str = None):
self.api_key = api_key or os.getenv('OPENAI_API_KEY')
openai.api_key = self.api_key
def supports_model(self, model: str) -> bool:
supported_models = [
'gpt-3.5-turbo', 'gpt-4', 'text-davinci-003',
'text-curie-001', 'text-babbage-001'
]
return model in supported_models
def generate_text(self,
prompt: str,
model: str = 'gpt-3.5-turbo',
**kwargs) -> str:
try:
# Adjust parameters based on model type
if model.startswith('gpt'):
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
**kwargs
)
return response.choices[0].message.content
else:
response = openai.Completion.create(
model=model,
prompt=prompt,
**kwargs
)
return response.choices[0].text.strip()
except Exception as e:
raise RuntimeError(f"OpenAI API Error: {str(e)}")
class AnthropicHandler(BaseAPIHandler):
def __init__(self, api_key: str = None):
self.api_key = api_key or os.getenv('ANTHROPIC_API_KEY')
self.client = anthropic.Anthropic(api_key=self.api_key)
def supports_model(self, model: str) -> bool:
supported_models = [
'claude-1', 'claude-2', 'claude-instant-1'
]
return model in supported_models
def generate_text(self,
prompt: str,
model: str = 'claude-2',
**kwargs) -> str:
try:
response = self.client.completions.create(
model=model,
prompt=prompt,
**kwargs
)
return response.completion
except Exception as e:
raise RuntimeError(f"Anthropic API Error: {str(e)}")