-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_interface.py
More file actions
81 lines (70 loc) · 2.99 KB
/
llm_interface.py
File metadata and controls
81 lines (70 loc) · 2.99 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
import os
import anthropic
from typing import Optional, List, Dict, Any
from dotenv import load_dotenv
from config import Config
class LLMInterface:
"""Interface for communicating with Anthropic's Claude API."""
def __init__(self, prompts_dir: str = None):
# Load environment variables from .env file
load_dotenv()
# Get API key from environment
api_key = os.getenv("ANTHROPIC_API_KEY")
if not api_key:
raise ValueError("ANTHROPIC_API_KEY environment variable is required")
self.client = anthropic.Anthropic(api_key=api_key)
if prompts_dir is None:
self.prompts_dir = os.path.join(os.path.dirname(__file__), 'prompts')
else:
self.prompts_dir = prompts_dir
self.system_prompt = self._load_prompt("system_constitution")
def _load_prompt(self, prompt_name: str):
"""Loads a prompt from a .txt file in the prompts directory."""
prompt_path = os.path.join(self.prompts_dir, f"{prompt_name}.txt")
try:
with open(prompt_path, 'r') as f:
return f.read()
except FileNotFoundError:
raise ValueError(f"Prompt file not found at: {prompt_path}")
def get_response(self, prompt_name: str, images: Optional[List[str]] = None,
placeholders: Optional[Dict[str, Any]] = None) -> Optional[str]:
"""
Sends a request to the LLM using a prompt from a .txt file.
Args:
prompt_name: Name of the prompt file (without .txt extension)
images: List of base64 encoded image strings
placeholders: Dictionary of placeholder values for prompt formatting
Returns:
The LLM response text, or None if an error occurred
Raises:
LLMError: If the LLM request fails
"""
user_prompt_template = self._load_prompt(prompt_name)
if placeholders:
user_prompt = user_prompt_template.format(**placeholders)
else:
user_prompt = user_prompt_template
content = [{"type": "text", "text": user_prompt}]
if images:
for img_data in images:
content.insert(0, {
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": img_data,
},
})
try:
message = self.client.messages.create(
model=Config.CLAUDE_MODEL,
max_tokens=Config.MAX_TOKENS,
system=self.system_prompt,
messages=[{"role": "user", "content": content}],
)
return message.content[0].text
except Exception as e:
print(f"An error occurred: {e}")
return None
if __name__ == '__main__':
print("LLMInterface class updated to use individual .txt files from the 'prompts/' directory.")