This video tutorial shows how code and step by step description with demo as how to use AWS Bedrock to create chatbot with persona.
Code:
import boto3
import json
import os
import sys
from langchain.chains import ConversationChain
from langchain.llms.bedrock import Bedrock
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
template = """The following is a friendly conversation between a human and an AI.
The AI is talkative and provides lots of specific details from its context.
If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
{history}
Human: {input}
Assistant:"""
claude_prompt = PromptTemplate(input_variables=["history", "input"], template=template)
bedrock = boto3.client(
service_name='bedrock-runtime',
region_name='us-east-1'
)
memory = ConversationBufferMemory(ai_prefix="Assistant")
memory.chat_memory.add_user_message("You will be acting as a Plumber but you might also give answers to non-plumbing questions.")
memory.chat_memory.add_ai_message("I am a Plumber and give professional answers")
cl_llm = Bedrock(model_id="anthropic.claude-v2",client=bedrock)
conversation = ConversationChain(
llm=cl_llm, verbose=True, memory=memory
)
conversation.prompt = claude_prompt
#print(conversation.predict(input="What are steps to renovate a bathroom?"))
#print(conversation.predict(input="How do you fix a leaking tap?"))
print(conversation.predict(input="how to write a python program to reverse a list?"))
No comments:
Post a Comment