forked from caio-moliveira/ai-agent-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_first_ai_agent.py
More file actions
59 lines (44 loc) · 1.66 KB
/
Copy pathmy_first_ai_agent.py
File metadata and controls
59 lines (44 loc) · 1.66 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
from langchain.chains import LLMChain
from langchain_aws import ChatBedrock # ✅ Updated Import
from langchain.prompts import PromptTemplate
import boto3
import os
import streamlit as st
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
AWS_REGION = os.getenv("AWS_REGION", "us-east-1")
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
if not AWS_ACCESS_KEY_ID or not AWS_SECRET_ACCESS_KEY:
raise ValueError("AWS credentials are missing. Please check your .env file.")
bedrock_client = boto3.client(
"bedrock-runtime",
region_name=AWS_REGION,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)
llm = ChatBedrock(
model_id="anthropic.claude-3-sonnet-20240229-v1:0", # set the foundation model
model_kwargs={
"temperature": 0,
"max_tokens": 1000,
# "topP": 0.5, # Removido Depreciated
# "maxTokenCount": 100, # Removido Depreciated
},
)
def my_chatbot(language, freeform_text):
prompt = PromptTemplate(
input_variables=["language", "freeform_text"],
template="You are a chatbot. You are in {language}.\n\n{freeform_text}",
)
bedrock_chain = LLMChain(llm=llm, prompt=prompt)
response = bedrock_chain({"language": language, "freeform_text": freeform_text})
return response
st.title("Bedrock Chatbot")
language = st.sidebar.selectbox("Language", ["english", "spanish"])
if language:
freeform_text = st.sidebar.text_area(label="What is your question?", max_chars=100)
if freeform_text:
response = my_chatbot(language, freeform_text)
st.write(response["text"])