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

# ChatSambanova 集成

> 使用 LangChain Python 与 ChatSambanova 聊天模型集成。

本文将帮助您开始使用 SambaNova [聊天模型](/oss/python/langchain/models/)。有关 `ChatSambaNova` 所有功能和配置的详细文档，请参阅 [API 参考](https://docs.sambanova.ai/cloud/docs/get-started/overview)。

**[SambaNova](https://sambanova.ai/)** 的 [SambaCloud](http://cloud.sambanova.ai?utm_source=langchain\&utm_medium=external\&utm_campaign=cloud_signup) 是一个用于使用开源模型进行推理的云平台。

## 概述

### 集成详情

| 类                                                                            | 包                                                                      | 可序列化 | JS 支持 |                                                  下载量                                                 |                                                 版本                                                |
| :--------------------------------------------------------------------------- | :--------------------------------------------------------------------- | :--: | :---: | :--------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------: |
| [`ChatSambaNova`](https://docs.sambanova.ai/cloud/docs/get-started/overview) | [`langchain-sambanova`](/oss/python/integrations/providers/sambanova/) | beta |   ❌   | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain_sambanova?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain_sambanova?style=flat-square\&label=%20) |

### 模型特性

| [工具调用](/oss/python/langchain/tools) | [结构化输出](/oss/python/langchain/structured-output) | [图像输入](/oss/python/langchain/messages#multimodal) | 音频输入 | 视频输入 | [令牌级流式传输](/oss/python/langchain/streaming/) | 原生异步 | [令牌使用量](/oss/python/langchain/models#token-usage) | [对数概率](/oss/python/langchain/models#log-probabilities) |
| :---------------------------------: | :----------------------------------------------: | :-----------------------------------------------: | :--: | :--: | :-----------------------------------------: | :--: | :-----------------------------------------------: | :----------------------------------------------------: |
|                  ✅                  |                         ✅                        |                         ✅                         |   ✅  |   ❌  |                      ✅                      |   ✅  |                         ✅                         |                            ❌                           |

## 设置

要访问 SambaNova 模型，您需要创建一个 [SambaCloud](http://cloud.sambanova.ai?utm_source=langchain\&utm_medium=external\&utm_campaign=cloud_signup) 账户，获取 API 密钥，并安装 `langchain_sambanova` 集成包。

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install langchain-sambanova
```

### 凭证

从 [cloud.sambanova.ai](http://cloud.sambanova.ai/apis?utm_source=langchain\&utm_medium=external\&utm_campaign=cloud_signupapis) 获取 API 密钥。完成后，设置 SAMBANOVA\_API\_KEY 环境变量：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

if not os.getenv("SAMBANOVA_API_KEY"):
    os.environ["SAMBANOVA_API_KEY"] = getpass.getpass(
        "输入您的 SambaNova API 密钥："
    )
```

要启用模型调用的自动追踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("输入您的 LangSmith API 密钥：")
```

### 安装

LangChain **SambaNova** 集成位于 `langchain_sambanova` 包中：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-sambanova
```

## 实例化

现在我们可以实例化模型对象并生成聊天补全：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_sambanova import ChatSambaNova

llm = ChatSambaNova(
    model="Meta-Llama-3.3-70B-Instruct",
    max_tokens=1024,
    temperature=0.7,
    top_p=0.01,
    # 其他参数...
)
```

## 调用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
messages = [
    (
        "system",
        "您是一个将英语翻译成法语的助手。"
        "请翻译用户的句子。",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
AIMessage(content="J'adore la programmation.", additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'usage': {'acceptance_rate': 7, 'completion_tokens': 8, 'completion_tokens_after_first_per_sec': 195.0204119588971, 'completion_tokens_after_first_per_sec_first_ten': 618.3422770734173, 'completion_tokens_per_sec': 53.25837044790076, 'end_time': 1731535338.1864908, 'is_last_response': True, 'prompt_tokens': 55, 'start_time': 1731535338.0133238, 'time_to_first_token': 0.13727331161499023, 'total_latency': 0.15021112986973353, 'total_tokens': 63, 'total_tokens_per_sec': 419.4096672772185}, 'model_name': 'Meta-Llama-3.1-70B-Instruct', 'system_fingerprint': 'fastcoe', 'created': 1731535338}, id='f04b7c2c-bc46-47e0-9c6b-19a002e8f390')
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(ai_msg.content)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
J'adore la programmation.
```

## 流式传输

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
system = "您是一个带有海盗口音的助手。"
human = "我想了解更多关于这种动物的信息：{animal}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])

chain = prompt | llm

for chunk in chain.stream({"animal": "owl"}):
    print(chunk.content, end="", flush=True)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
Yer lookin' fer some knowledge about owls, eh? Alright then, matey, settle yerself down with a pint o' grog and listen close.

Owls be a fascinatin' lot, with their big round eyes and silent wings. They be birds o' prey, which means they hunt other creatures fer food. There be over 220 species o' owls, rangin' in size from the tiny Elf Owl (which be smaller than a parrot) to the Great Grey Owl (which be as big as a small eagle).

One o' the most interestin' things about owls be their eyes. They be huge, with some species havin' eyes that be as big as their brains! This lets 'em see in the dark, which be perfect fer nocturnal huntin'. They also have special feathers on their faces that help 'em hear better, and their ears be specially designed to pinpoint sounds.

Owls be known fer their silent flight, which be due to the special shape o' their wings. They be able to fly without makin' a sound, which be perfect fer sneakin' up on prey. They also be very agile, with some species able to fly through tight spaces and make sharp turns.

Some o' the most common species o' owls include:

* Barn Owl: A medium-sized owl with a heart-shaped face and a screechin' call.
* Tawny Owl: A large owl with a distinctive hootin' call and a reddish-brown plumage.
* Great Horned Owl: A big owl with ear tufts and a deep hootin' call.
* Snowy Owl: A white owl with a round face and a soft, hootin' call.

Owls be found all over the world, in a variety o' habitats, from forests to deserts. They be an important part o' many ecosystems, helpin' to keep populations o' small mammals and birds under control.

So there ye have it, matey! Owls be amazin' creatures, with their big eyes, silent wings, and sharp talons. Now go forth and spread the word about these fascinatin' birds!
```

## 异步

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
prompt = ChatPromptTemplate.from_messages(
    [
        (
            "human",
            "{country}的首都是什么？",
        )
    ]
)

chain = prompt | llm
await chain.ainvoke({"country": "France"})
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
AIMessage(content='法国的首都是巴黎。', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'usage': {'acceptance_rate': 1, 'completion_tokens': 7, 'completion_tokens_after_first_per_sec': 442.126212227688, 'completion_tokens_after_first_per_sec_first_ten': 0, 'completion_tokens_per_sec': 46.28540439646366, 'end_time': 1731535343.0321083, 'is_last_response': True, 'prompt_tokens': 42, 'start_time': 1731535342.8808727, 'time_to_first_token': 0.137664794921875, 'total_latency': 0.15123558044433594, 'total_tokens': 49, 'total_tokens_per_sec': 323.99783077524563}, 'model_name': 'Meta-Llama-3.1-70B-Instruct', 'system_fingerprint': 'fastcoe', 'created': 1731535342}, id='c4b8c714-df38-4206-9aa8-fc8231f7275a')
```

## 异步流式传输

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
prompt = ChatPromptTemplate.from_messages(
    [
        (
            "human",
            "用少于{num_words}个词向我解释{topic}",
        )
    ]
)
chain = prompt | llm

async for chunk in chain.astream({"num_words": 30, "topic": "quantum computers"}):
    print(chunk.content, end="", flush=True)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
量子计算机使用量子比特（qubits）处理信息，利用叠加和纠缠特性，针对某些复杂问题，其计算速度比经典计算机呈指数级提升。
```

## 工具调用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from datetime import datetime

from langchain.messages import HumanMessage, ToolMessage
from langchain.tools import tool


@tool
def get_time(kind: str = "both") -> str:
    """返回当前日期、当前时间或两者。
    参数：
        kind(str): date、time 或 both
    返回：
        str: 当前日期、当前时间或两者
    """
    if kind == "date":
        date = datetime.now().strftime("%m/%d/%Y")
        return f"当前日期：{date}"
    elif kind == "time":
        time = datetime.now().strftime("%H:%M:%S")
        return f"当前时间：{time}"
    else:
        date = datetime.now().strftime("%m/%d/%Y")
        time = datetime.now().strftime("%H:%M:%S")
        return f"当前日期：{date}, 当前时间：{time}"


tools = [get_time]


def invoke_tools(tool_calls, messages):
    available_functions = {tool.name: tool for tool in tools}
    for tool_call in tool_calls:
        selected_tool = available_functions[tool_call["name"]]
        tool_output = selected_tool.invoke(tool_call["args"])
        print(f"工具输出：{tool_output}")
        messages.append(ToolMessage(tool_output, tool_call_id=tool_call["id"]))
    return messages
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
llm_with_tools = llm.bind_tools(tools=tools)
messages = [
    HumanMessage(
        content="我需要安排一个两周后的会议。"
        "您能告诉我会议的确切日期吗？"
    )
]
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
response = llm_with_tools.invoke(messages)
while len(response.tool_calls) > 0:
    print(f"中间模型响应：{response.tool_calls}")
    messages.append(response)
    messages = invoke_tools(response.tool_calls, messages)
    response = llm_with_tools.invoke(messages)

print(f"最终响应：{response.content}")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
中间模型响应：[{'name': 'get_time', 'args': {'kind': 'date'}, 'id': 'call_7352ce7a18e24a7c9d', 'type': 'tool_call'}]
工具输出：当前日期：11/13/2024
最终响应：会议应安排在2024年11月13日之后的两周。
```

## 结构化输出

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from pydantic import BaseModel, Field


class Joke(BaseModel):
    """讲给用户的笑话。"""

    setup: str = Field(description="笑话的铺垫")
    punchline: str = Field(description="笑话的包袱")


structured_llm = llm.with_structured_output(Joke)

structured_llm.invoke("讲一个关于猫的笑话")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
Joke(setup='为什么猫要加入乐队？', punchline='因为它想成为打击乐手（purr-cussionist）！')
```

## 图像输入

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
multimodal_llm = ChatSambaNova(
    model="Llama-4-Maverick-17B-128E-Instruct",
    max_tokens=1024,
    temperature=0.7,
    top_p=0.01,
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import base64

import httpx

image_url = (
    "https://images.pexels.com/photos/147411/italy-mountains-dawn-daybreak-147411.jpeg"
)
image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")

message = HumanMessage(
    content=[
        {"type": "text", "text": "用一句话描述这张图片中的天气"},
        {
            "type": "image_url",
            "image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
        },
    ],
)
response = multimodal_llm.invoke([message])
print(response.content)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
这张图片中的天气是宁静祥和的氛围，蓝天白云，暗示着宜人的天气，温度适中，微风轻拂。
```

***

## API 参考

有关 `SambaNova` 所有功能和配置的详细文档，请参阅 API 参考：[docs.sambanova.ai/cloud/docs/get-started/overview](https://docs.sambanova.ai/cloud/docs/get-started/overview)

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\chat\sambanova.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
