> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ChatOCIModelDeployment 集成

> 使用 LangChain 集成 ChatOCIModelDeployment 对话模型。

这将帮助您快速入门 (/oss/langchain/models)。如需了解所有 `ChatOCIModelDeployment` 功能和配置的详细文档，请参考 [langchain-oci 包](https://github.com/oracle/langchain-oracle/tree/main/libs/oci)。

[OCI Data Science](https://docs.oracle.com/en-us/iaas/data-science/using/home.htm) 是 Oracle Cloud Infrastructure 平台上的全托管无服务器平台，专为数据科学团队设计，用于在 Oracle 云中构建、训练和管理机器学习模型。您可以通过 [AI 快速操作](https://blogs.oracle.com/ai-and-datascience/post/ai-quick-actions-in-oci-data-science) 在 [OCI Data Science 模型部署服务](https://docs.oracle.com/en-us/iaas/data-science/using/model-dep-about.htm) 中轻松部署大语言模型（LLMs）。您可以选择使用 vLLM 或 TGI 等流行推理框架部署模型。默认情况下，模型部署端点会模拟 OpenAI API 协议。

> 如需最新更新、示例和实验性功能，请查看 [ADS LangChain 集成](https://accelerated-data-science.readthedocs.io/en/latest/user_guide/large_language_model/langchain_models.html)。

## 概览

### 集成细节

| 类别 | 包 | 可序列化 | JS 支持 | 下载量 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: |
| [`ChatOCIModelDeployment`](https://github.com/oracle/langchain-oracle/tree/main/libs/oci) | [`langchain-oci`](https://pypi.org/project/langchain-oci/) | beta | ❌ | ![PyPI - 下载量](https://img.shields.io/pypi/dm/langchain-oci?style=flat-square\&label=%20) | ![PyPI - 版本](https://img.shields.io/pypi/v/langchain-oci?style=flat-square\&label=%20) |

### 模型特性

| [工具调用](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md) | [结构化输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/structured-output.md) | [图像输入](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/messages.md#多模态) | 音频输入 | 视频输入 | [Token级流式输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/streaming.md) | 原生异步 | [Token使用量](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#token-使用) | [对数概率](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#对数概率) |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| 依赖 | 依赖 | 依赖 | 依赖 | 依赖 | ✅ | ✅ | ✅ | ✅ |

部分模型特性（包括工具调用、结构化输出和多模态输入）取决于部署的模型版本。

## 部署

要使用 `ChatOCIModelDeployment`，需部署具有对话完成端点的聊天模型，并安装 `langchain-oci` 和 `oracle-ads` 包。

您可以通过 [AI 快速操作](https://github.com/oracle-samples/oci-data-science-ai-samples/blob/main/ai-quick-actions/model-deployment-tips.md) 在 OCI Data Science 模型部署服务中快速部署基础模型。如需更多部署示例，请访问 [Oracle GitHub 示例仓库](https://github.com/oracle-samples/oci-data-science-ai-samples/tree/main/ai-quick-actions)。

### 政策

确保已配置访问 [OCI Data Science 模型部署端点](https://docs.oracle.com/en-us/iaas/data-science/using/model-dep-policies-auth.htm#model_dep_policies_auth__predict-endpoint) 所需的 [政策](https://docs.oracle.com/en-us/iaas/data-science/using/model-dep-policies-auth.htm#model_dep_policies_auth__predict-endpoint)。

### 凭据

可通过 Oracle ADS 设置身份验证。在 OCI Data Science 笔记本会话中工作时，可以使用资源主元访问其他 OCI 资源。

```python theme={null}
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")
```

alternatively，您也可以通过以下环境变量配置凭据。例如，使用特定配置文件的API密钥：

```python theme={null}
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 文档](https://accelerated-data-science.readthedocs.io/en/latest/user_guide/cli/authentication.html) 以查看更多选项。

### 安装

LangChain OCIModelDeployment 集成位于 `langchain-oci` 包中。

```bash pip theme={null}
pip install -qU langchain-oci oracle-ads
```

```bash uv theme={null}
uv add langchain-oci oracle-ads
```

## 实例化

您可以使用通用 `ChatOCIModelDeployment` 或框架特定类如 `ChatOCIModelDeploymentVLLM` 实例化模型。

* 使用 `ChatOCIModelDeployment` 当您需要一个通用的模型部署入口点。您可以通过 `model_kwargs` 在此类实例化过程中传递模型参数。这提供了灵活性和配置便捷性，无需依赖框架特定细节。

```python theme={null}
from langchain_oci 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`：当您正在使用特定框架（例如 `vLLM`）时适用，可直接通过构造函数传递模型参数，简化配置流程。

```python theme={null}
from langchain_oci 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",
)
```

## 调用

```python theme={null}
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
```

```text theme={null}
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')
```

```python theme={null}
print(ai_msg.content)
```

```text theme={null}
J'adore programmer.
```

## 链接

```python theme={null}
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.",
    }
)
```

```text theme={null}
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')
```

## 异步调用

```python theme={null}
from langchain_oci 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",
    }
)
```

```text theme={null}
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')
```

## 流式调用

```python theme={null}
import sys

from langchain_oci 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()
```

```text theme={null}
1. California
2. Texas
3. Florida
4. New York
5. Illinois
```

## 结构化输出

```python theme={null}
from langchain_oci 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()
```

```text theme={null}
{'setup': 'Why did the cat get stuck in the tree?',
 'punchline': 'Because it was chasing its tail!'}
```

## API 参考

要查看所有功能及配置的完整细节，请参考 [langchain-oci 包](https://github.com/oracle/langchain-oracle/tree/main/libs/oci) 文档中各类的说明：

* `ChatOCIModelDeployment`
* `ChatOCIModelDeploymentVLLM`
* `ChatOCIModelDeploymentTGI`

***

> 官方原文：[src/oss/python/integrations/chat/oci\_data\_science.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/chat/oci_data_science.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.