oci_generative_ai
Oracle Cloud Infrastructure Generative AI
Oracle Cloud Infrastructure (OCI) Generative AI 是一項完全受管的服務,提供一組最先進、可自訂的大型語言模型 (LLM),涵蓋廣泛的用例,並可透過單一 API 取得。使用 OCI Generative AI 服務,您可以存取隨時可用的預先訓練模型,或根據您在專用 AI 叢集上的自有資料,建立及託管您自己的微調自訂模型。服務和 API 的詳細文件可在此處和此處取得:這裡 和 這裡。
本筆記本說明如何搭配 LangChain 使用 OCI 的 Generative AI 完整模型。
設定
確保已安裝 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
auth_file_location="MY_CONFIG_FILE_LOCATION", # replace with file location where profile name configs present
)
專用 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,
auth_file_location="MY_CONFIG_FILE_LOCATION", # replace with file location where profile name configs present
provider="MODEL_PROVIDER", # e.g., "cohere" or "meta"
context_size="MODEL_CONTEXT_SIZE", # e.g., 128000
)