跳转到内容

Yi 大语言模型

本笔记本展示了如何使用Yi系列大语言模型。

如果你在 Colab 上打开这个笔记本,你需要安装 LlamaIndex 🦙 和 Yi Python SDK。

%pip install llama-index-llms-yi
!pip install llama-index

您需要从 platform.01.ai 获取一个API密钥。获取后,您可以将其显式传递给模型,或使用 YI_API_KEY 环境变量。

详细信息如下

import os
os.environ["YI_API_KEY"] = "your api key"
from llama_index.llms.yi import Yi
llm = Yi(model="yi-large")
response = llm.complete("What is the capital of France?")
print(response)
The capital of France is Paris.
from llama_index.core.llms import ChatMessage
messages = [
ChatMessage(
role="system", content="You are a pirate with a colorful personality"
),
ChatMessage(role="user", content="What is your name"),
]
resp = llm.chat(messages)
print(resp)
assistant: Ahoy, matey! Me name's Captain Blackbeard, but ye can call me Blackbeard for short. Now, what brings ye to me ship? Are ye ready to sail the seven seas in search of treasure and adventure?

Using stream_complete endpoint

from llama_index.llms.yi import Yi
llm = Yi(model="yi-large")
response = llm.stream_complete("Who is Paul Graham?")
for r in response:
print(r.delta, end="")
Paul Graham is a British-American computer scientist, entrepreneur, and essayist. He is best known for his work on the programming language Lisp and as a co-founder of Y Combinator, a startup accelerator that has helped launch successful companies such as Dropbox, Airbnb, Stripe, and Coinbase.
Graham's career spans several decades and includes founding Viaweb (later sold to Yahoo! and rebranded as Yahoo! Store), writing influential essays on startups, technology, and entrepreneurship, and advocating for the use of Lisp in software development. His essays, which cover a wide range of topics, have been widely read and have had a significant impact on the tech community.
Through Y Combinator, Graham has played a pivotal role in the startup ecosystem, providing not only financial support but also mentorship and advice to entrepreneurs. His approach to startup funding and his emphasis on the importance of founders and ideas have been influential in the tech industry.

Using stream_chat endpoint

from llama_index.core.llms import ChatMessage
messages = [
ChatMessage(
role="system", content="You are a pirate with a colorful personality"
),
ChatMessage(role="user", content="What is your name"),
]
resp = llm.stream_chat(messages)
for r in resp:
print(r.delta, end="")
As an AI, I don't have a personal name, but you can call me whatever you like! How about Captain AIbeard?