主题
如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。
%pip install llama-index-llms-upstage llama-index基本用法
使用提示词调用 complete
import os
os.environ["UPSTAGE_API_KEY"] = "YOUR_API_KEY"from llama_index.llms.upstage import Upstage
llm = Upstage(
model="solar-mini",
# api_key="YOUR_API_KEY" # uses UPSTAGE_API_KEY env var by default
)
resp = llm.complete("Paul Graham is ")print(resp)Paul Graham is a computer scientist, entrepreneur, and essayist. He is best known as the co-founder of the venture capital firm Y Combinator, which has funded and incubated many successful startups. He is also the author of several influential essays on entrepreneurship, startup culture, and technology.使用消息列表调用 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: I am Captain Redbeard, the fearless pirate!流式传输
使用 stream_complete 端点
resp = llm.stream_complete("Paul Graham is ")for r in resp:
print(r.delta, end="")Paul Graham is a computer scientist, entrepreneur, and essayist. He is best known for co-founding the startup accelerator Y Combinator, which has helped launch some of the most successful tech companies in the world, including Airbnb, Dropbox, and Stripe. He is also the author of several influential essays on startup culture and entrepreneurship, including "How to Start a Startup" and "Hackers & Painters."使用 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="")I am Captain Redbeard, the fearless pirate!函数调用
Upstage 模型原生支持函数调用。这可以方便地与 LlamaIndex 工具抽象集成,让你可以将任意 Python 函数插入 LLM。
from pydantic import BaseModel
from llama_index.core.tools import FunctionTool
class Song(BaseModel):
"""A song with name and artist"""
name: str
artist: str
def generate_song(name: str, artist: str) -> Song:
"""Generates a song with provided name and artist."""
return Song(name=name, artist=artist)
tool = FunctionTool.from_defaults(fn=generate_song)from llama_index.llms.upstage import Upstage
llm = Upstage()
response = llm.predict_and_call([tool], "Generate a song")
print(str(response))name='My Song' artist='John Doe'我们也可以进行多个函数调用。
llm = Upstage()
response = llm.predict_and_call(
[tool],
"Generate five songs from the Beatles",
allow_parallel_tool_calls=True,
)
for s in response.sources:
print(f"Name: {s.tool_name}, Input: {s.raw_input}, Output: {str(s)}")Name: generate_song, Input: {'args': (), 'kwargs': {'name': 'Beatles', 'artist': 'Beatles'}}, Output: name='Beatles' artist='Beatles'异步
from llama_index.llms.upstage import Upstage
llm = Upstage()resp = await llm.acomplete("Paul Graham is ")print(resp)Paul Graham is a computer scientist, entrepreneur, and essayist. He is best known as the co-founder of the startup accelerator Y Combinator, which has helped launch and fund many successful tech companies. He is also the author of several influential essays on startups, entrepreneurship, and technology, including "How to Start a Startup" and "Hackers & Painters."resp = await llm.astream_complete("Paul Graham is ")async for delta in resp:
print(delta.delta, end="")Paul Graham is a computer scientist, entrepreneur, and essayist. He is best known as the co-founder of the startup accelerator Y Combinator, which has helped launch some of the most successful tech companies in the world, including Airbnb, Dropbox, and Stripe. Graham is also a prolific writer, and his essays on topics such as startup advice, artificial intelligence, and the future of work have been widely read and influential in the tech industry.也支持异步函数调用。
llm = Upstage()
response = await llm.apredict_and_call([tool], "Generate a song")
print(str(response))name='My Song' artist='Me'