跳转到内容

Maritalk

MariTalk是由巴西公司Maritaca AI开发的助手。 MariTalk基于经过专门训练以良好理解葡萄牙语的语言模型。

本笔记本通过两个示例演示了如何将 MariTalk 与 Llama Index 结合使用:

  1. 通过聊天方法获取宠物名称建议;
  2. 使用完整方法通过少量示例将电影评论分类为负面或正面。

如果你在 Colab 上打开这个笔记本,可能需要安装 LlamaIndex。

!pip install llama-index
!pip install llama-index-llms-maritalk
!pip install asyncio

您需要一个API密钥,该密钥可从 chat.maritaca.ai 获取(“Chaves da API”部分)。

from llama_index.core.llms import ChatMessage
from llama_index.llms.maritalk import Maritalk
import asyncio
# To customize your API key, do this
# otherwise it will lookup MARITALK_API_KEY from your env variable
llm = Maritalk(api_key="<your_maritalk_api_key>", model="sabia-2-medium")
# Call chat with a list of messages
messages = [
ChatMessage(
role="system",
content="You are an assistant specialized in suggesting pet names. Given the animal, you must suggest 4 names.",
),
ChatMessage(role="user", content="I have a dog."),
]
# Sync chat
response = llm.chat(messages)
print(response)
# Async chat
async def get_dog_name(llm, messages):
response = await llm.achat(messages)
print(response)
asyncio.run(get_dog_name(llm, messages))

对于涉及生成长文本的任务,例如撰写长篇文档或翻译大型文件,在文本生成过程中分段接收响应,而非等待完整文本生成完毕,可能更为有利。这种方式使应用程序响应更迅速、效率更高,尤其当生成的文本体量庞大时。我们提供两种方案来满足这一需求:一种是同步方式,另一种是异步方式。

# Sync streaming chat
response = llm.stream_chat(messages)
for chunk in response:
print(chunk.delta, end="", flush=True)
# Async streaming chat
async def get_dog_name_streaming(llm, messages):
async for chunk in await llm.astream_chat(messages):
print(chunk.delta, end="", flush=True)
asyncio.run(get_dog_name_streaming(llm, messages))

我们建议在使用模型进行少样本示例时使用 llm.complete() 方法

prompt = """Classifique a resenha de filme como "positiva" ou "negativa".
Resenha: Gostei muito do filme, é o melhor do ano!
Classe: positiva
Resenha: O filme deixa muito a desejar.
Classe: negativa
Resenha: Apesar de longo, valeu o ingresso..
Classe:"""
# Sync complete
response = llm.complete(prompt)
print(response)
# Async complete
async def classify_review(llm, prompt):
response = await llm.acomplete(prompt)
print(response)
asyncio.run(classify_review(llm, prompt))
# Sync streaming complete
response = llm.stream_complete(prompt)
for chunk in response:
print(chunk.delta, end="", flush=True)
# Async streaming complete
async def classify_review_streaming(llm, prompt):
async for chunk in await llm.astream_complete(prompt):
print(chunk.delta, end="", flush=True)
asyncio.run(classify_review_streaming(llm, prompt))