Back to Llama Index

AlibabaCloud-PaiEas

docs/examples/llm/paieas.ipynb

0.14.212.1 KB
Original Source

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

AlibabaCloud-PaiEas

If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.

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

Basic Usage

You will need to get an API key and url from AlibabaCloud PAI Eas. Once you have one, you can either pass it explicity to the model, or use the PAIEAS_API_KEY and PAIEAS_API_BASE environment variable.

python
!export PAIEAS_API_KEY=your_service_token
!export PAIEAS_API_BASE=your_access_address

Call complete with a prompt

python
from llama_index.llms.paieas import PaiEas

llm = PaiEas()
resp = llm.complete("Write a poem about a magic backpack")
python
print(resp)

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)
python
print(resp)

Streaming

Using stream_complete endpoint

python
resp = llm.stream_complete("Paul Graham is ")
python
for r in resp:
    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="")

Async

python
resp = await llm.acomplete("Paul Graham is ")
python
print(resp)
python
resp = await llm.astream_complete("Paul Graham is ")
python
async for delta in resp:
    print(delta.delta, end="")