이 문서는 OCIModelDeployment chat models 시작하기를 도와드립니다. 모든 ChatOCIModelDeployment 기능 및 구성에 대한 자세한 문서는 API reference를 참조하세요. OCI Data Science는 데이터 과학 팀이 Oracle Cloud Infrastructure에서 머신러닝 모델을 구축, 훈련 및 관리할 수 있는 완전 관리형 서버리스 플랫폼입니다. AI Quick Actions를 사용하여 OCI Data Science Model Deployment Service에 LLM을 쉽게 배포할 수 있습니다. vLLM 또는 TGI와 같은 인기 있는 추론 프레임워크로 모델을 배포할 수 있습니다. 기본적으로 모델 배포 endpoint는 OpenAI API protocol을 모방합니다.
최신 업데이트, 예제 및 실험적 기능은 ADS LangChain Integration을 참조하세요.

Overview

Integration details

ClassPackageLocalSerializableJS supportDownloadsVersion
ChatOCIModelDeploymentlangchain-communitybetaPyPI - DownloadsPyPI - Version

Model features

Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingNative asyncToken usageLogprobs
dependsdependsdependsdependsdependsdepends
tool calling, structured output, JSON mode 및 multi-modal inputs를 포함한 일부 모델 기능은 배포된 모델에 따라 달라집니다.

Setup

ChatOCIModelDeployment를 사용하려면 chat completion endpoint가 있는 chat model을 배포하고 langchain-community, langchain-openaioracle-ads integration package를 설치해야 합니다. OCI Data Science Model deployment에서 AI Quick Actions를 사용하여 foundation model을 쉽게 배포할 수 있습니다. 추가 배포 예제는 Oracle GitHub samples repository를 참조하세요.

Policies

OCI Data Science Model Deployment endpoint에 액세스하는 데 필요한 policies가 있는지 확인하세요.

Credentials

Oracle ADS를 통해 인증을 설정할 수 있습니다. OCI Data Science Notebook Session에서 작업할 때 resource principal을 활용하여 다른 OCI 리소스에 액세스할 수 있습니다.
import ads

# Set authentication through ads
# Use resource principal are operating within a
# OCI service that has resource principal based
# authentication configured
ads.set_auth("resource_principal")
또는 다음 환경 변수를 사용하여 credentials를 구성할 수 있습니다. 예를 들어, 특정 profile과 함께 API key를 사용하려면:
import os

# Set authentication through environment variables
# Use API Key setup when you are working from a local
# workstation or on platform which does not support
# resource principals.
os.environ["OCI_IAM_TYPE"] = "api_key"
os.environ["OCI_CONFIG_PROFILE"] = "default"
os.environ["OCI_CONFIG_LOCATION"] = "~/.oci"
더 많은 옵션은 Oracle ADS docs를 참조하세요.

Installation

LangChain OCIModelDeployment integration은 langchain-community package에 있습니다. 다음 명령은 langchain-community와 필요한 dependencies를 설치합니다.
pip install -qU langchain-community langchain-openai oracle-ads

Instantiation

generic ChatOCIModelDeployment 또는 ChatOCIModelDeploymentVLLM과 같은 framework 특정 class로 모델을 인스턴스화할 수 있습니다.
  • ChatOCIModelDeployment 사용: 모델 배포를 위한 generic entry point가 필요할 때 사용합니다. 이 class를 인스턴스화하는 동안 model_kwargs를 통해 model parameter를 전달할 수 있습니다. 이를 통해 framework별 세부 사항에 의존하지 않고도 유연하고 쉽게 구성할 수 있습니다.
from langchain_community.chat_models import ChatOCIModelDeployment

# Create an instance of OCI Model Deployment Endpoint
# Replace the endpoint uri with your own
# Using generic class as entry point, you will be able
# to pass model parameters through model_kwargs during
# instantiation.
chat = ChatOCIModelDeployment(
    endpoint="https://modeldeployment.<region>.oci.customer-oci.com/<ocid>/predict",
    streaming=True,
    max_retries=1,
    model_kwargs={
        "temperature": 0.2,
        "max_tokens": 512,
    },  # other model params...
    default_headers={
        "route": "/v1/chat/completions",
        # other request headers ...
    },
)
  • ChatOCIModelDeploymentVLLM과 같은 framework 특정 class 사용: 특정 framework(예: vLLM)로 작업하고 constructor를 통해 model parameter를 직접 전달하여 설정 프로세스를 간소화해야 할 때 적합합니다.
from langchain_community.chat_models import ChatOCIModelDeploymentVLLM

# Create an instance of OCI Model Deployment Endpoint
# Replace the endpoint uri with your own
# Using framework specific class as entry point, you will
# be able to pass model parameters in constructor.
chat = ChatOCIModelDeploymentVLLM(
    endpoint="https://modeldeployment.<region>.oci.customer-oci.com/<md_ocid>/predict",
)

Invocation

messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]

ai_msg = chat.invoke(messages)
ai_msg
AIMessage(content="J'adore programmer.", response_metadata={'token_usage': {'prompt_tokens': 44, 'total_tokens': 52, 'completion_tokens': 8}, 'model_name': 'odsc-llm', 'system_fingerprint': '', 'finish_reason': 'stop'}, id='run-ca145168-efa9-414c-9dd1-21d10766fdd3-0')
print(ai_msg.content)
J'adore programmer.

Chaining

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are a helpful assistant that translates {input_language} to {output_language}.",
        ),
        ("human", "{input}"),
    ]
)

chain = prompt | chat
chain.invoke(
    {
        "input_language": "English",
        "output_language": "German",
        "input": "I love programming.",
    }
)
AIMessage(content='Ich liebe Programmierung.', response_metadata={'token_usage': {'prompt_tokens': 38, 'total_tokens': 48, 'completion_tokens': 10}, 'model_name': 'odsc-llm', 'system_fingerprint': '', 'finish_reason': 'stop'}, id='run-5dd936b0-b97e-490e-9869-2ad3dd524234-0')

Asynchronous calls

from langchain_community.chat_models import ChatOCIModelDeployment

system = "You are a helpful translator that translates {input_language} to {output_language}."
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])

chat = ChatOCIModelDeployment(
    endpoint="https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<ocid>/predict"
)
chain = prompt | chat

await chain.ainvoke(
    {
        "input_language": "English",
        "output_language": "Chinese",
        "text": "I love programming",
    }
)
AIMessage(content='我喜欢编程', response_metadata={'token_usage': {'prompt_tokens': 37, 'total_tokens': 50, 'completion_tokens': 13}, 'model_name': 'odsc-llm', 'system_fingerprint': '', 'finish_reason': 'stop'}, id='run-a2dc9393-f269-41a4-b908-b1d8a92cf827-0')

Streaming calls

import os
import sys

from langchain_community.chat_models import ChatOCIModelDeployment
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(
    [("human", "List out the 5 states in the United State.")]
)

chat = ChatOCIModelDeployment(
    endpoint="https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<ocid>/predict"
)

chain = prompt | chat

for chunk in chain.stream({}):
    sys.stdout.write(chunk.content)
    sys.stdout.flush()
1. California
2. Texas
3. Florida
4. New York
5. Illinois

Structured output

from langchain_community.chat_models import ChatOCIModelDeployment
from pydantic import BaseModel


class Joke(BaseModel):
    """A setup to a joke and the punchline."""

    setup: str
    punchline: str


chat = ChatOCIModelDeployment(
    endpoint="https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<ocid>/predict",
)
structured_llm = chat.with_structured_output(Joke, method="json_mode")
output = structured_llm.invoke(
    "Tell me a joke about cats, respond in JSON with `setup` and `punchline` keys"
)

output.dict()
{'setup': 'Why did the cat get stuck in the tree?',
 'punchline': 'Because it was chasing its tail!'}

API reference

모든 기능 및 구성에 대한 포괄적인 세부 정보는 각 class의 API reference 문서를 참조하세요:
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I