-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathllmapi.py
65 lines (47 loc) · 1.65 KB
/
llmapi.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
from anthropic import Anthropic
from openai import OpenAI
import os
from typing import Optional
from dotenv import load_dotenv
load_dotenv()
class ChatResponse:
def __init__(self, role, content):
self.role = role
self.content = content
def get_message(self):
return {"role": self.role, "content": self.content}
class LLM:
def __init__(self, client, system_prompt):
pass
def get_response(self, messages) -> Optional[ChatResponse]:
pass
class Claude(LLM):
client: Anthropic
def __init__(self, system_prompt):
self.client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
self.system_prompt = system_prompt
def get_response(self, messages):
message = self.client.messages.create(
system=self.system_prompt,
model="claude-3-opus-20240229",
max_tokens=1024,
messages=messages,
temperature=0.0,
)
return ChatResponse(message.role, message.content[0].text)
class GPT(LLM):
client: OpenAI
def _make_messages(self, messages):
return [{"role": "system", "content": self.system_prompt}] + messages
def __init__(self, system_prompt) -> None:
self.client = OpenAI(api_key=os.getenv("OPENAI_KEY"))
self.system_prompt = system_prompt
def get_response(self, messages):
response = self.client.chat.completions.create(
model="gpt-4-turbo-preview",
temperature=0.0,
messages=self._make_messages(messages),
)
return ChatResponse(
response.choices[0].message.role, response.choices[0].message.content
)