跳到主要内容
科大讯飞提供的 SparkLLM 聊天模型 API。欲了解更多信息,请参阅科大讯飞开放平台

基本用法

"""For basic init and call"""
from langchain_community.chat_models import ChatSparkLLM
from langchain.messages import HumanMessage

chat = ChatSparkLLM(
    spark_app_id="<app_id>", spark_api_key="<api_key>", spark_api_secret="<api_secret>"
)
message = HumanMessage(content="Hello")
chat([message])
AIMessage(content='Hello! How can I help you today?')
  • 科大讯飞 SparkLLM API 控制台获取 SparkLLM 的 app_id、api_key 和 api_secret(欲了解更多信息,请参阅科大讯飞 SparkLLM 介绍),然后设置环境变量IFLYTEK_SPARK_APP_IDIFLYTEK_SPARK_API_KEYIFLYTEK_SPARK_API_SECRET,或在创建ChatSparkLLM时像上面的演示那样传递参数。

带流式传输的 ChatSparkLLM

chat = ChatSparkLLM(
    spark_app_id="<app_id>",
    spark_api_key="<api_key>",
    spark_api_secret="<api_secret>",
    streaming=True,
)
for chunk in chat.stream("Hello!"):
    print(chunk.content, end="")
Hello! How can I help you today?

适用于 v2

"""For basic init and call"""
from langchain_community.chat_models import ChatSparkLLM
from langchain.messages import HumanMessage

chat = ChatSparkLLM(
    spark_app_id="<app_id>",
    spark_api_key="<api_key>",
    spark_api_secret="<api_secret>",
    spark_api_url="wss://spark-api.xf-yun.com/v2.1/chat",
    spark_llm_domain="generalv2",
)
message = HumanMessage(content="Hello")
chat([message])

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.