MariTalk is an assistant developed by the Brazilian company Maritaca AI.
MariTalk is based on language models that have been specially trained to understand Portuguese well.This notebook demonstrates how to use MariTalk with LangChain through two examples:
A simple example of how to use MariTalk to perform a task.
LLM + RAG: The second example shows how to answer a question whose answer is found in a long document that does not fit within the token limit of MariTalk. For this, we will use a simple searcher (BM25) to first search the document for the most relevant sections and then feed them to MariTalk for answering.
Let’s define our language model, ChatMaritalk, and configure it with your API key.
Copy
Ask AI
from langchain_community.chat_models import ChatMaritalkfrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.prompts.chat import ChatPromptTemplatellm = ChatMaritalk( model="sabia-2-medium", # Available models: sabia-2-small and sabia-2-medium api_key="", # Insert your API key here temperature=0.7, max_tokens=100,)output_parser = StrOutputParser()chat_prompt = ChatPromptTemplate.from_messages( [ ( "system", "You are an assistant specialized in suggesting pet names. Given the animal, you must suggest 4 names.", ), ("human", "I have a {animal}"), ])chain = chat_prompt | llm | output_parserresponse = chain.invoke({"animal": "dog"})print(response) # should answer something like "1. Max\n2. Bella\n3. Charlie\n4. Rocky"
For tasks involving the generation of long text, such as creating an extensive article or translating a large document, it can be advantageous to receive the response in parts, as the text is generated, instead of waiting for the complete text. This makes the application more responsive and efficient, especially when the generated text is extensive. We offer two approaches to meet this need: one synchronous and another asynchronous.
from langchain_core.messages import HumanMessagemessages = [HumanMessage(content="Suggest 3 names for my dog")]for chunk in llm.stream(messages): print(chunk.content, end="", flush=True)
from langchain_core.messages import HumanMessageasync def async_invoke_chain(animal: str): messages = [HumanMessage(content=f"Suggest 3 names for my {animal}")] async for chunk in llm._astream(messages): print(chunk.message.content, end="", flush=True)await async_invoke_chain("dog")
The first step is to create a database with the information from the notice. For this, we will download the notice from the COMVEST website and segment the extracted text into 500-character windows.
Now that we have our database, we need a searcher. For this example, we will use a simple BM25 as a search system, but this could be replaced by any other searcher (such as search via embeddings).
Copy
Ask AI
from langchain_community.retrievers import BM25Retrieverretriever = BM25Retriever.from_documents(texts)
Now that we have our searcher, we just need to implement a prompt specifying the task and invoke the chain.
Copy
Ask AI
from langchain.chains.question_answering import load_qa_chainprompt = """Baseado nos seguintes documentos, responda a pergunta abaixo.{context}Pergunta: {query}"""qa_prompt = ChatPromptTemplate.from_messages([("human", prompt)])chain = load_qa_chain(llm, chain_type="stuff", verbose=True, prompt=qa_prompt)query = "Qual o tempo máximo para realização da prova?"docs = retriever.invoke(query)chain.invoke( {"input_documents": docs, "query": query}) # Should output something like: "O tempo máximo para realização da prova é de 5 horas."
Assistant
Responses are generated using AI and may contain mistakes.