Skip to main content
Seekr 为结构化、可解释且透明的 AI 交互提供 AI 驱动的解决方案。
本指南提供了开始使用 Seekr 聊天模型的快速概览。有关所有 ChatSeekrFlow 功能和配置的详细文档,请前往 API 参考

概述

ChatSeekrFlow 类封装了托管在 SeekrFlow 上的聊天模型端点,实现与 LangChain 应用的无缝集成。

集成详情

可序列化下载量版本
ChatSeekrFlowseekraibetaPyPI - DownloadsPyPI - Version

模型功能

工具调用结构化输出图像输入音频输入视频输入Token 级流式输出原生异步Token 用量对数概率

支持的方法

ChatSeekrFlow 支持 ChatModel 的所有方法,异步 API 除外

端点要求

ChatSeekrFlow 封装的服务端点必须具有 OpenAI 兼容的聊天输入/输出格式。它可用于:
  1. 经过微调的 Seekr 模型
  2. 自定义 SeekrFlow 模型
  3. 使用 Seekr 检索系统的 RAG 启用模型
对于异步使用,请参阅 AsyncChatSeekrFlow(即将推出)。

在 LangChain 中开始使用 ChatSeekrFlow

本 notebook 介绍如何在 LangChain 中使用 SeekrFlow 作为聊天模型。

设置

确保已安装必要的依赖项:
pip install seekrai langchain langchain-community
您还必须拥有 Seekr 的 API key 来验证请求。
# Standard library
import getpass
import os

# Third-party
from langchain.prompts import ChatPromptTemplate
from langchain.schema import HumanMessage
from langchain_core.runnables import RunnableSequence

# OSS SeekrFlow integration
from langchain_seekrflow import ChatSeekrFlow
from seekrai import SeekrFlow

API Key 设置

您需要将 API key 设置为环境变量以验证请求。 运行以下单元格。 或在运行查询前手动分配:
SEEKR_API_KEY = "your-api-key-here"
os.environ["SEEKR_API_KEY"] = getpass.getpass("Enter your Seekr API key:")

实例化

os.environ["SEEKR_API_KEY"]
seekr_client = SeekrFlow(api_key=SEEKR_API_KEY)

llm = ChatSeekrFlow(
    client=seekr_client, model_name="meta-llama/Meta-Llama-3-8B-Instruct"
)

调用

response = llm.invoke([HumanMessage(content="Hello, Seekr!")])
print(response.content)
Hello there! I'm Seekr, nice to meet you! What brings you here today? Do you have a question, or are you looking for some help with something? I'm all ears (or rather, all text)!

链式调用

prompt = ChatPromptTemplate.from_template("Translate to French: {text}")

chain: RunnableSequence = prompt | llm
result = chain.invoke({"text": "Good morning"})
print(result)
content='The translation of "Good morning" in French is:\n\n"Bonne journée"' additional_kwargs={} response_metadata={}
def test_stream():
    """Test synchronous invocation in streaming mode."""
    print("\n🔹 Testing Sync `stream()` (Streaming)...")

    for chunk in llm.stream([HumanMessage(content="Write me a haiku.")]):
        print(chunk.content, end="", flush=True)


# ✅ Ensure streaming is enabled
llm = ChatSeekrFlow(
    client=seekr_client,
    model_name="meta-llama/Meta-Llama-3-8B-Instruct",
    streaming=True,  # ✅ Enable streaming
)

# ✅ Run sync streaming test
test_stream()
🔹 Testing Sync `stream()` (Streaming)...
Here is a haiku:

Golden sunset fades
Ripples on the quiet lake
Peaceful evening sky

错误处理与调试

# Define a minimal mock SeekrFlow client
class MockSeekrClient:
    """Mock SeekrFlow API client that mimics the real API structure."""

    class MockChat:
        """Mock Chat object with a completions method."""

        class MockCompletions:
            """Mock Completions object with a create method."""

            def create(self, *args, **kwargs):
                return {
                    "choices": [{"message": {"content": "Mock response"}}]
                }  # Mimic API response

        completions = MockCompletions()

    chat = MockChat()


def test_initialization_errors():
    """Test that invalid ChatSeekrFlow initializations raise expected errors."""

    test_cases = [
        {
            "name": "Missing Client",
            "args": {"client": None, "model_name": "seekrflow-model"},
            "expected_error": "SeekrFlow client cannot be None.",
        },
        {
            "name": "Missing Model Name",
            "args": {"client": MockSeekrClient(), "model_name": ""},
            "expected_error": "A valid model name must be provided.",
        },
    ]

    for test in test_cases:
        try:
            print(f"Running test: {test['name']}")
            faulty_llm = ChatSeekrFlow(**test["args"])

            # If no error is raised, fail the test
            print(f"❌ Test '{test['name']}' failed: No error was raised!")
        except Exception as e:
            error_msg = str(e)
            assert test["expected_error"] in error_msg, f"Unexpected error: {error_msg}"
            print(f"✅ Expected Error: {error_msg}")


# Run test
test_initialization_errors()
Running test: Missing Client
✅ Expected Error: SeekrFlow client cannot be None.
Running test: Missing Model Name
✅ Expected Error: A valid model name must be provided.

API 参考