Dockerfile and pulumi stack for infra
This commit is contained in:
13
agent/Dockerfile
Normal file
13
agent/Dockerfile
Normal file
@@ -0,0 +1,13 @@
|
||||
FROM public.ecr.aws/lambda/python:3.13
|
||||
|
||||
# Copy requirements.txt
|
||||
COPY requirements.txt ${LAMBDA_TASK_ROOT}
|
||||
|
||||
# Install the specified packages
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
# Copy function code
|
||||
COPY agent.py ${LAMBDA_TASK_ROOT}
|
||||
|
||||
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
|
||||
CMD ["agent.hello" ]
|
||||
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
from langchain_core.tools import tool
|
||||
from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
|
||||
from langchain_core.prompts import ChatPromptTemplate,MessagesPlaceholder, PromptTemplate
|
||||
@@ -16,6 +17,14 @@ from langchain_core.agents import AgentAction, AgentFinish
|
||||
from langchain.agents.output_parsers import ReActSingleInputOutputParser
|
||||
from langchain.tools import Tool
|
||||
import os
|
||||
def find_tool_by_name(tools: list[Tool],tool_name:str):
|
||||
for tool in tools:
|
||||
if tool.name==tool_name:
|
||||
print(tool.name)
|
||||
print("\n\n")
|
||||
return tool
|
||||
raise ValueError(f"Tool with name {tool_name} not found")
|
||||
def agent_call(event,context):
|
||||
llm = ChatBedrock(
|
||||
model_id="arn:aws:bedrock:us-east-1:654654422992:application-inference-profile/d9blf0g3fzqz",
|
||||
region_name="us-east-1",
|
||||
@@ -31,12 +40,6 @@ retriever = AmazonKnowledgeBasesRetriever(
|
||||
retrieval_config={"vectorSearchConfiguration": {"numberOfResults": 4}},
|
||||
|
||||
)
|
||||
def find_tool_by_name(tools: list[Tool],tool_name:str):
|
||||
for tool in tools:
|
||||
if tool.name==tool_name:
|
||||
return tool
|
||||
raise ValueError(f"Tool with name {tool_name} not found")
|
||||
if __name__=="__main__":
|
||||
print("Hello React Langhain")
|
||||
tools=[retriever.as_tool()]
|
||||
template="""Você é um assistente para alunos de diversos campus diferentes do instituto federal de são paulo, sua função é responder perguntas
|
||||
@@ -62,13 +65,12 @@ Question: {input}
|
||||
Chat history:{chat_history}
|
||||
Thought: {agent_scratchpad}
|
||||
"""
|
||||
|
||||
prompt=PromptTemplate.from_template(template=template).partial(tools=render_text_description(tools), tool_names=','.join([t.name for t in tools]))
|
||||
#llm=ChatOpenAI(model="gpt-4o-mini",temperature=0,stop_sequences=["\nObservation:"])
|
||||
intermediate_steps=[]
|
||||
|
||||
agent= {"input": lambda x:x["input"],"agent_scratchpad": lambda x:format_log_to_str(x["agent_scratchpad"]),"chat_history":lambda x:x["chat_history"]}|prompt | llm
|
||||
agent_step: Union[AgentAction,AgentFinish]=agent.invoke({"input": "Quanto é o valor do auxilio moradia?","agent_scratchpad":intermediate_steps,"chat_history":{"role":"user","content":""}})
|
||||
agent= {"input": lambda x:x["input"],"agent_scratchpad": lambda x:format_log_to_str(x["agent_scratchpad"]),"chat_history":lambda x:x["chat_history"]}|prompt | llm|ReActJsonSingleInputOutputParser()
|
||||
agent_step: Union[AgentAction,AgentFinish]=agent.invoke({"input": "Que dia é hoje?","agent_scratchpad":intermediate_steps,"chat_history":{"role":"user","content":"sou do campus sao paulo"}})
|
||||
#print(agent_step)
|
||||
if isinstance(agent_step,AgentAction):
|
||||
tool_name=agent_step.tool
|
||||
@@ -77,5 +79,10 @@ Thought: {agent_scratchpad}
|
||||
observation=tool_to_use.func(str(tool_input))
|
||||
print(f"{observation=}")
|
||||
intermediate_steps.append((agent_step,str(observation)))
|
||||
agent_step: Union[AgentAction,AgentFinish]=agent.invoke({"input": "Quanto é o valor do auxiliomoradia?","agent_scratchpad":intermediate_steps,"chat_history":{"role":"user","content":""}})
|
||||
print(agent_step)
|
||||
agent_step: Union[AgentAction,AgentFinish]=agent.invoke({"input": "Quanto é o valor do auxilio moradia?","agent_scratchpad":intermediate_steps,"chat_history":{"role":"user","content":"sou do campus sao paulo"}})
|
||||
return agent_step
|
||||
def hello(event,context):
|
||||
return{
|
||||
"statusCode":200,
|
||||
"body":json.dumps("hello_world")
|
||||
}
|
||||
|
||||
4
agent/requirements.txt
Normal file
4
agent/requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
langchain_core
|
||||
langchain
|
||||
langchain_aws
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
config:
|
||||
aws:region: us-east-1
|
||||
9
infra/Pulumi.ifsp_chatbot_ecr.yaml
Normal file
9
infra/Pulumi.ifsp_chatbot_ecr.yaml
Normal file
@@ -0,0 +1,9 @@
|
||||
config:
|
||||
ifsp-chatbot-poc:entity_extraction_dev: ecr
|
||||
ifsp-chatbot-poc:environment: dev
|
||||
ifsp-chatbot-poc:ecr:
|
||||
entity_extraction:
|
||||
image_mutability: MUTABLE
|
||||
name: br-edu-ifsp-ifsp-ret-ecr-chatbot-editais-dev
|
||||
ifsp-chatbot-poc:project: chatbot-editais
|
||||
aws:region: us-east-1
|
||||
@@ -1,22 +1,34 @@
|
||||
"""An AWS Python Pulumi program"""
|
||||
|
||||
import pulumi
|
||||
from pulumi_aws import aws
|
||||
import pulumi_aws as aws
|
||||
from pulumi_aws import s3
|
||||
import pulumi_aws_apigateway as apigateway
|
||||
import json
|
||||
role = aws.iam.Role("mylambda-role",
|
||||
assume_role_policy=json.dumps({
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [{
|
||||
"Effect": "Allow",
|
||||
"Principal": { "Service": "lambda.amazonaws.com" },
|
||||
"Action": "sts:AssumeRole"
|
||||
}]
|
||||
})
|
||||
)
|
||||
caller_identity = aws.get_caller_identity()
|
||||
account_id = caller_identity.account_id
|
||||
|
||||
config = pulumi.Config()
|
||||
project = config.require("project")
|
||||
environment = config.require("environment")
|
||||
|
||||
ecr_config = config.require_object("ecr")["entity_extraction"]
|
||||
|
||||
ecr_repo = aws.ecr.Repository(ecr_config['name'],
|
||||
name=ecr_config['name'],
|
||||
encryption_configurations=[{
|
||||
"encryption_type": "AES256",
|
||||
}],
|
||||
image_scanning_configuration={
|
||||
"scan_on_push": False,
|
||||
},
|
||||
image_tag_mutability=ecr_config['image_mutability'],
|
||||
opts = pulumi.ResourceOptions(protect=True))
|
||||
|
||||
|
||||
pulumi.export("url", pulumi.Output.concat("ECR REPO ID:", ecr_repo.id))
|
||||
|
||||
# Create an AWS resource (S3 Bucket)
|
||||
bucket = s3.Bucket('br-edu-ifsp-ifsp-ret-s3-bucket-chatbot-editais-d',
|
||||
"""bucket = s3.Bucket('br-edu-ifsp-ifsp-ret-s3-bucket-chatbot-editais-d',
|
||||
tags={
|
||||
"nome":"bucket-chatbot-editais",
|
||||
"ambiente":"dev",
|
||||
@@ -31,20 +43,7 @@ bucket = s3.Bucket('br-edu-ifsp-ifsp-ret-s3-bucket-chatbot-editais-d',
|
||||
"ia":"sim",
|
||||
"modelo":"claude-sonnet-4"
|
||||
})
|
||||
f = aws.lambda_.Function("mylambda",
|
||||
runtime=aws.lambda_.Runtime.PYTHON3D8,
|
||||
code=pulumi.AssetArchive({
|
||||
".": pulumi.FileArchive("./handler"),
|
||||
}),
|
||||
timeout=300,
|
||||
handler="handler.handler",
|
||||
role=role.arn,
|
||||
opts=pulumi.ResourceOptions(depends_on=[policy]),
|
||||
)
|
||||
|
||||
api = apigateway.RestAPI('api', routes=[
|
||||
apigateway.RouteArgs(path="/", method="GET", event_handler=f),
|
||||
])
|
||||
|
||||
# Export the name of the bucket
|
||||
pulumi.export('bucket_name', bucket.id)
|
||||
pulumi.export('bucket_name', bucket.id)"""
|
||||
|
||||
Reference in New Issue
Block a user