Last active
July 30, 2024 21:34
-
-
Save gusdelact/7f3eaa55d3bd545fc9caa39b75aab1b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#simple chatbot con Langchain y usando Amazon Bedrock. | |
#las llaves AWS_ACCESS_KEY_ID y AWS_SECRET_ACCESS_KEY así como AWS_DEFAULT_REGION se deben | |
#fijar como variables de ambiente | |
import gradio as gr | |
import random | |
import langchain | |
import langchain_community | |
from langchain_aws import ChatBedrock | |
from langchain.chains import ConversationChain | |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder | |
#para manejar la memoria del chat | |
import uuid | |
from langchain_community.chat_message_histories import ChatMessageHistory | |
from langchain_core.chat_history import BaseChatMessageHistory | |
from langchain_core.runnables.history import RunnableWithMessageHistory | |
#para langchain 0.2 | |
store = {} | |
#funcion que retorna un id de sesion usando uuid | |
def get_chat_session_id(): | |
return str(uuid.uuid4()) | |
def get_session_history(session_id: str) -> BaseChatMessageHistory: | |
if session_id not in store: | |
store[session_id] = ChatMessageHistory() | |
return store[session_id] | |
def init(): | |
#definir chat con memoria | |
chat = ChatBedrock( | |
model_id="anthropic.claude-3-sonnet-20240229-v1:0", | |
model_kwargs={"temperature": 0.1} | |
) | |
prompt = ChatPromptTemplate.from_messages( | |
[ | |
( | |
"system", | |
"You're an assistant who's good at {ability}. Respond in 20 words or fewer", | |
), | |
MessagesPlaceholder(variable_name="history"), | |
("human", "{input}"), | |
] | |
) | |
runnable = prompt | chat | |
chat_with_message_history = RunnableWithMessageHistory( | |
runnable, | |
get_session_history, | |
input_messages_key="input", | |
history_messages_key="history", | |
) | |
return chat_with_message_history | |
def bedrock_response(message,history): | |
the_ability= "Filosofia" | |
response=the_chat.invoke( | |
{"ability": the_ability, "input": message}, | |
config={"configurable": {"session_id": id_session}}, | |
) | |
print(type(response)) | |
print(response) | |
return response.content | |
#iniciar | |
id_session=get_chat_session_id() | |
the_chat = init() | |
#retry clear y undo no están soportados para esta versión | |
demo = gr.ChatInterface(bedrock_response,clear_btn=None,retry_btn=None,undo_btn=None) | |
if __name__ == "__main__": | |
demo.launch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment