Skip to content
LlamaIndex 文档版本 跟随上游 源快照 e23cc410 更新于 2026-10-18

本 notebook 展示如何使用 Yi 系列 LLM。

如果你在 colab 上打开此 Notebook,需要安装 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"

使用提示词调用 complete

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.

使用消息列表调用 chat

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?

流式传输

使用 stream_complete 端点

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.

使用 stream_chat 端点

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?

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。