主题
Oracle Cloud Infrastructure (OCI)Data Science 是一个完全托管、无服务器的平台,供数据科学团队在 Oracle Cloud Infrastructure 中构建、训练和管理机器学习模型。
它提供 AI Quick Actions,可用于在 OCI Data Science 中部署、评估和微调基础 LLM 模型。AI Quick Actions 面向希望快速利用 AI 能力的用户。它们旨在通过提供简化、无代码且高效的基础模型工作环境,将基础模型扩展到更广泛的用户群体。AI Quick Actions 可以从 Data Science Notebook 访问。
有关如何使用 AI Quick Actions 在 OCI Data Science 中部署 LLM 模型的详细文档,请见此处和此处。
本 notebook 介绍如何将 OCI 的 Data Science 模型与 LlamaIndex 一起使用。
设置
如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。
%pip install llama-index-llms-oci-data-science!pip install llama-index你还需要安装 oracle-ads SDK。
!pip install -U oracle-ads身份认证
LlamaIndex 支持的身份验证方法与其他 OCI 服务相同,遵循标准 SDK 身份验证方法,具体包括 API Key、会话令牌、实例主体和资源主体。更多细节可在此处找到。请确保具备访问 OCI Data Science Model Deployment 端点所需的策略。oracle-ads 有助于简化 OCI Data Science 内的身份验证。
基本用法
要通过 LlamaIndex 使用 OCI Data Science AI 提供的 LLM,只需使用你的 Data Science 模型部署端点和模型 ID 初始化 OCIDataScience 接口。默认情况下,AI Quick Actions 中所有已部署模型都会获得 odsc-model ID。不过,此 ID 可以在部署期间更改。
使用提示词调用 complete
import ads
from llama_index.llms.oci_data_science import OCIDataScience
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
)
response = llm.complete("Tell me a joke")
print(response)使用消息列表调用 chat
import ads
from llama_index.llms.oci_data_science import OCIDataScience
from llama_index.core.base.llms.types import ChatMessage
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
)
response = llm.chat(
[
ChatMessage(role="user", content="Tell me a joke"),
ChatMessage(
role="assistant", content="Why did the chicken cross the road?"
),
ChatMessage(role="user", content="I don't know, why?"),
]
)
print(response)流式传输
使用专用流式端点
from llama_index.llms.oci_data_science import OCIDataScience
import ads
ads.set_auth(auth="security_token", profile="OC1")
llm = OCIDataScience(
endpoint="https://<MD_OCID>/predictWithResponseStream",
model="odsc-llm",
)
prompt = "What is the capital of France?"
response = llm.stream_complete(prompt)
for chunk in response:
print(chunk.delta, end="")使用 stream_complete 端点
import ads
from llama_index.llms.oci_data_science import OCIDataScience
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
)
for chunk in llm.stream_complete("Tell me a joke"):
print(chunk.delta, end="")使用 stream_chat 端点
import ads
from llama_index.llms.oci_data_science import OCIDataScience
from llama_index.core.base.llms.types import ChatMessage
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
)
response = llm.stream_chat(
[
ChatMessage(role="user", content="Tell me a joke"),
ChatMessage(
role="assistant", content="Why did the chicken cross the road?"
),
ChatMessage(role="user", content="I don't know, why?"),
]
)
for chunk in response:
print(chunk.delta, end="")异步
使用提示词调用 acomplete
import ads
from llama_index.llms.oci_data_science import OCIDataScience
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
)
response = await llm.acomplete("Tell me a joke")
print(response)使用消息列表调用 achat
import ads
from llama_index.llms.oci_data_science import OCIDataScience
from llama_index.core.base.llms.types import ChatMessage
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
)
response = await llm.achat(
[
ChatMessage(role="user", content="Tell me a joke"),
ChatMessage(
role="assistant", content="Why did the chicken cross the road?"
),
ChatMessage(role="user", content="I don't know, why?"),
]
)
print(response)使用 astream_complete 端点
import ads
from llama_index.llms.oci_data_science import OCIDataScience
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
)
async for chunk in await llm.astream_complete("Tell me a joke"):
print(chunk.delta, end="")使用 astream_chat 端点
import ads
from llama_index.llms.oci_data_science import OCIDataScience
from llama_index.core.base.llms.types import ChatMessage
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
)
response = await llm.stream_chat(
[
ChatMessage(role="user", content="Tell me a joke"),
ChatMessage(
role="assistant", content="Why did the chicken cross the road?"
),
ChatMessage(role="user", content="I don't know, why?"),
]
)
async for chunk in response:
print(chunk.delta, end="")配置模型
import ads
from llama_index.llms.oci_data_science import OCIDataScience
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
temperature=0.2,
max_tokens=500,
timeout=120,
context_window=2500,
additional_kwargs={
"top_p": 0.75,
"logprobs": True,
"top_logprobs": 3,
},
)
response = llm.chat(
[
ChatMessage(role="user", content="Tell me a joke"),
]
)
print(response)函数调用
AI Quick Actions 提供预构建的服务容器,使大语言模型的部署和服务变得非常容易。服务容器中使用 vLLM(一种面向 LLM 的高吞吐、内存高效的推理和服务引擎)或 TGI(一种面向流行开源 LLM 的高性能文本生成服务器)之一来托管模型,创建的端点支持 OpenAI API 协议。这使模型部署可以作为使用 OpenAI API 的应用的直接替代品。如果部署的模型支持函数调用,那么通过 llm 上的 predict_and_call 函数与 LlamaIndex 工具集成,可以附加任意工具,并让 LLM 决定要调用哪些工具(如果有)。
import ads
from llama_index.llms.oci_data_science import OCIDataScience
from llama_index.core.tools import FunctionTool
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
temperature=0.2,
max_tokens=500,
timeout=120,
context_window=2500,
additional_kwargs={
"top_p": 0.75,
"logprobs": True,
"top_logprobs": 3,
},
)
def multiply(a: float, b: float) -> float:
print(f"---> {a} * {b}")
return a * b
def add(a: float, b: float) -> float:
print(f"---> {a} + {b}")
return a + b
def subtract(a: float, b: float) -> float:
print(f"---> {a} - {b}")
return a - b
def divide(a: float, b: float) -> float:
print(f"---> {a} / {b}")
return a / b
multiply_tool = FunctionTool.from_defaults(fn=multiply)
add_tool = FunctionTool.from_defaults(fn=add)
sub_tool = FunctionTool.from_defaults(fn=subtract)
divide_tool = FunctionTool.from_defaults(fn=divide)
response = llm.predict_and_call(
[multiply_tool, add_tool, sub_tool, divide_tool],
user_msg="Calculate the result of `8 + 2 - 6`.",
verbose=True,
)
print(response)使用 FunctionAgent
import ads
from llama_index.llms.oci_data_science import OCIDataScience
from llama_index.core.tools import FunctionTool
from llama_index.core.agent.workflow import FunctionAgent
ads.set_auth(auth="security_token", profile="<replace-with-your-profile>")
llm = OCIDataScience(
model="odsc-llm",
endpoint="https://<MD_OCID>/predict",
temperature=0.2,
max_tokens=500,
timeout=120,
context_window=2500,
additional_kwargs={
"top_p": 0.75,
"logprobs": True,
"top_logprobs": 3,
},
)
def multiply(a: float, b: float) -> float:
print(f"---> {a} * {b}")
return a * b
def add(a: float, b: float) -> float:
print(f"---> {a} + {b}")
return a + b
def subtract(a: float, b: float) -> float:
print(f"---> {a} - {b}")
return a - b
def divide(a: float, b: float) -> float:
print(f"---> {a} / {b}")
return a / b
multiply_tool = FunctionTool.from_defaults(fn=multiply)
add_tool = FunctionTool.from_defaults(fn=add)
sub_tool = FunctionTool.from_defaults(fn=subtract)
divide_tool = FunctionTool.from_defaults(fn=divide)
agent = FunctionAgent(
tools=[multiply_tool, add_tool, sub_tool, divide_tool],
llm=llm,
)
response = await agent.run(
"Calculate the result of `8 + 2 - 6`. Use tools. Return the calculated result."
)
print(response)