跳转到内容

数据砖块

与 Databricks LLMs API 集成。

如果您在 Colab 上打开这个笔记本,您可能需要安装 LlamaIndex 🦙。

% pip install llama-index-llms-databricks
!pip install llama-index
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.
Terminal window
export DATABRICKS_TOKEN=<your api key>
export DATABRICKS_SERVING_ENDPOINT=<your api serving endpoint>

或者,您可以在初始化LLM时传递您的API密钥和服务端点:

llm = Databricks(
model="databricks-dbrx-instruct",
api_key="your_api_key",
api_base="https://[your-work-space].cloud.databricks.com/serving-endpoints/",
)

可用的LLM模型列表可在此处找到。

response = llm.complete("Explain the importance of open source LLMs")
print(response)
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)

Using stream_complete endpoint

response = llm.stream_complete("Explain the importance of open source LLMs")
for r in response:
print(r.delta, end="")

Using stream_chat endpoint

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="")