DataBricks¶
先决条件¶
Databricks个人访问令牌用于查询和访问Databricks模型服务端点。
在支持的区域中拥有Databricks工作区,以便使用Foundation Model APIs按令牌付费。
设置¶
如果您在colab上打开这个笔记本,您可能需要安装LlamaIndex 🦙。
In [ ]:
Copied!
% pip install llama-index-llms-databricks
% pip install llama-index-llms-databricks
In [ ]:
Copied!
!pip install llama-index
!pip install llama-index
In [ ]:
Copied!
from llama_index.llms.databricks import DataBricks
from llama_index.llms.databricks import DataBricks
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
export DATABRICKS_API_KEY=<你的API密钥>
export DATABRICKS_API_BASE=<你的API服务端点>
或者,你可以在初始化时将你的API密钥和服务端点传递给LLM:
In [ ]:
Copied!
llm = DataBricks(
model="databricks-dbrx-instruct",
api_key="your_api_key",
api_base="https://[your-work-space].cloud.databricks.com/serving-endpoints/[your-serving-endpoint]",
)
llm = DataBricks(
model="databricks-dbrx-instruct",
api_key="your_api_key",
api_base="https://[your-work-space].cloud.databricks.com/serving-endpoints/[your-serving-endpoint]",
)
可以在这里找到可用的LLM模型列表。
In [ ]:
Copied!
response = llm.complete("Explain the importance of open source LLMs")
response = llm.complete("Explain the importance of open source LLMs")
In [ ]:
Copied!
print(response)
print(response)
使用消息列表调用 chat
¶
In [ ]:
Copied!
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)
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)
In [ ]:
Copied!
print(resp)
print(resp)
Streaming¶
使用 stream_complete
终端点
In [ ]:
Copied!
response = llm.stream_complete("Explain the importance of open source LLMs")
response = llm.stream_complete("Explain the importance of open source LLMs")
In [ ]:
Copied!
for r in response:
print(r.delta, end="")
for r in response:
print(r.delta, end="")
使用 stream_chat
端点
In [ ]:
Copied!
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)
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)
In [ ]:
Copied!
for r in resp:
print(r.delta, end="")
for r in resp:
print(r.delta, end="")