Back to Llama Index

Yi LLMs

docs/examples/llm/yi.ipynb

0.14.211.9 KB
Original Source

<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/yi.ipynb" target="_parent"></a>

Yi LLMs

This notebook shows how to use Yi series LLMs.

If you're opening this Notebook on colab, you will need to install LlamaIndex 🦙 and the Yi Python SDK.

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

Fundamental Usage

You will need to get an API key from platform.01.ai. Once you have one, you can either pass it explicity to the model, or use the YI_API_KEY environment variable.

The details are as follows

python
import os

os.environ["YI_API_KEY"] = "your api key"

Call complete with a prompt

python
from llama_index.llms.yi import Yi

llm = Yi(model="yi-large")
response = llm.complete("What is the capital of France?")
print(response)

Call chat with a list of messages

python
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)

Streaming

Using stream_complete endpoint

python
from llama_index.llms.yi import Yi

llm = Yi(model="yi-large")
response = llm.stream_complete("Who is Paul Graham?")
python
for r in response:
    print(r.delta, end="")

Using stream_chat endpoint

python
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)
python
for r in resp:
    print(r.delta, end="")