-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
55 lines (43 loc) · 1.53 KB
/
tools.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
import json
import os
import boto3
from typing import Union
from dotenv import load_dotenv
load_dotenv()
def ask_model(messages: str, instructions: str) -> str:
"""
Generates responses from a language model based on chat messages and instructions.
Args:
messages (str): The chat messages to be used as context for generating responses.
instructions (str): Additional instructions or question to provide to the model.
Returns:
str: The generated response from the language model.
"""
if not messages:
return "No messages was found. Consider using other chat."
prompt = str(messages) + instructions
prompt_config = {
"inputText": prompt,
"textGenerationConfig": {
"maxTokenCount": 1000,
"stopSequences": [],
"temperature": 1,
"topP": 1,
},
}
body = json.dumps(prompt_config)
bedrock_runtime = boto3.client(
service_name='bedrock-runtime',
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
region_name='us-east-1'
)
model_id = "amazon.titan-text-lite-v1"
accept = "application/json"
content_type = "application/json"
response = bedrock_runtime.invoke_model(
body=body, modelId=model_id, accept=accept, contentType=content_type
)
response_body = json.loads(response.get("body").read())
results = response_body.get("results")[0].get("outputText")
return results