-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllm.py
46 lines (36 loc) · 1.31 KB
/
llm.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
import base64
import logging
import os
from typing import Callable, List, Dict, Optional, Tuple
from langchain_aws import ChatBedrock
from langchain_core.messages import AIMessage, ToolMessage
from aws_lambda_powertools.metrics import MetricResolution, MetricUnit
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
CLAUDE_35_SONNET_V2_MODEL_ID = "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
logger = logging.getLogger(__name__)
class LLM:
def __init__(self):
logger.info("Initializing ClaudeLangchain")
# Set region from environment variable
os.environ['AWS_DEFAULT_REGION'] = os.getenv('REGION', 'us-east-1')
def chat_completion(
self,
messages: list,
model_id: str = CLAUDE_35_SONNET_V2_MODEL_ID,
temperature: float = 0.1,
max_tokens: int = 8192,
) -> str:
try:
logger.info(f"Calling Claude API with model {model_id}")
client = ChatBedrock(
model_id=model_id,
temperature=temperature,
max_tokens=max_tokens,
)
response: AIMessage = client.invoke(messages)
return response.content
except Exception as e:
logger.error(f"Error during Claude API call: {e}")
raise