oci_generative_ai
Oracle Cloud Infrastructure Generative AI
Oracle Cloud Infrastructure (OCI) Generative AI 是一項完全託管的服務,提供一組最先進、可自訂的大型語言模型 (LLM),涵蓋廣泛的使用案例,並可透過單一 API 取得。使用 OCI Generative AI 服務,您可以存取現成的預先訓練模型,或根據您在專用 AI 叢集上的自有資料,建立和託管您自己微調的自訂模型。服務和 API 的詳細文件請參閱此處和此處。
本筆記本說明如何將 OCI 的 Generative AI 完整模型與 LangChain 一起使用。
設定
請確保已安裝 oci sdk 和 langchain-community 套件
!pip install -U oci langchain-community
用法
from langchain_community.llms.oci_generative_ai import OCIGenAI
llm = OCIGenAI(
model_id="cohere.command",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="MY_OCID",
model_kwargs={"temperature": 0, "max_tokens": 500},
)
response = llm.invoke("Tell me one fact about earth", temperature=0.7)
print(response)
API 參考:OCIGenAI
使用提示範本進行鏈結
from langchain_core.prompts import PromptTemplate
llm = OCIGenAI(
model_id="cohere.command",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="MY_OCID",
model_kwargs={"temperature": 0, "max_tokens": 500},
)
prompt = PromptTemplate(input_variables=["query"], template="{query}")
llm_chain = prompt | llm
response = llm_chain.invoke("what is the capital of france?")
print(response)
API 參考:PromptTemplate
串流
llm = OCIGenAI(
model_id="cohere.command",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="MY_OCID",
model_kwargs={"temperature": 0, "max_tokens": 500},
)
for chunk in llm.stream("Write me a song about sparkling water."):
print(chunk, end="", flush=True)
身分驗證
LlamaIndex 支援的身分驗證方法與其他 OCI 服務使用的方法相同,並遵循標準 SDK 身分驗證方法,特別是 API 金鑰、工作階段權杖、執行個體主體和資源主體。
API 金鑰是上述範例中使用的預設身分驗證方法。以下範例示範如何使用不同的身分驗證方法 (工作階段權杖)
llm = OCIGenAI(
model_id="cohere.command",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="MY_OCID",
auth_type="SECURITY_TOKEN",
auth_profile="MY_PROFILE", # replace with your profile name
)
專用 AI 叢集
若要存取託管在專用 AI 叢集中的模型,請建立端點,其指派的 OCID (目前字首為 ‘ocid1.generativeaiendpoint.oc1.us-chicago-1’) 會用作您的模型 ID。
存取託管在專用 AI 叢集中的模型時,您需要使用兩個額外的必要參數 ("provider" 和 "context_size") 初始化 OCIGenAI 介面。
llm = OCIGenAI(
model_id="ocid1.generativeaiendpoint.oc1.us-chicago-1....",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="DEDICATED_COMPARTMENT_OCID",
auth_profile="MY_PROFILE", # replace with your profile name,
provider="MODEL_PROVIDER", # e.g., "cohere" or "meta"
context_size="MODEL_CONTEXT_SIZE", # e.g., 128000
)