Kackle commited on
Commit
1cf66c7
·
verified ·
1 Parent(s): fa2ae82

Create nova_agent.py

Browse files
Files changed (1) hide show
  1. nova_agent.py +69 -0
nova_agent.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import boto3
3
+ import json
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+
8
+ class NovaLiteAgent:
9
+ def __init__(self):
10
+ print("NovaLiteAgent initialized.")
11
+
12
+ # Get AWS credentials from environment variables
13
+ aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
14
+ aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
15
+
16
+ # Initialize the AWS client
17
+ boto3.client(
18
+ 's3',
19
+ aws_access_key_id=aws_access_key_id,
20
+ aws_secret_access_key=aws_secret_access_key
21
+ )
22
+ session = boto3.session.Session()
23
+
24
+ self.bedrock_client = boto3.client(
25
+ service_name='bedrock-runtime',
26
+ region_name='us-east-1'
27
+ )
28
+
29
+ self.model_id = "amazon.nova-lite-v1:0"
30
+ self.content_type = "application/json"
31
+ self.accept = "application/json"
32
+
33
+ async def __call__(self, question: str) -> str:
34
+ print(f"NovaLiteAgent received question (first 50 chars): {question[:50]}...")
35
+
36
+ try:
37
+ # Prepare the request payload for Nova Lite
38
+ payload = {
39
+ "messages": [
40
+ {
41
+ "role": "user",
42
+ "content": [{
43
+ "text": question
44
+ }]
45
+ }
46
+ ],
47
+ "inferenceConfig": {
48
+ "max_new_tokens": 1000,
49
+ "temperature": 0.1
50
+ }
51
+ }
52
+
53
+ # Call Nova Lite model
54
+ response = self.bedrock_client.invoke_model(
55
+ modelId=self.model_id,
56
+ contentType=self.content_type,
57
+ accept=self.accept,
58
+ body=json.dumps(payload)
59
+ )
60
+
61
+ # Parse response
62
+ response_body = json.loads(response['body'].read())
63
+ answer = response_body['output']['message']['content'][0]['text']
64
+
65
+ return answer.strip()
66
+
67
+ except Exception as e:
68
+ print(f"Error calling Nova Lite: {e}")
69
+ return f"I apologize, but I'm currently experiencing technical difficulties: {e}"