Skip to main content
Open In ColabOpen on GitHub

SparkLLM 聊天

SparkLLM聊天模型API由科大讯飞提供。如需了解更多信息,请参阅科大讯飞开放平台

基本用法

"""For basic init and call"""
from langchain_community.chat_models import ChatSparkLLM
from langchain_core.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?')
  • iFlyTek SparkLLM API控制台 (查看更多信息,请参见 iFlyTek SparkLLM 介绍 ) 获取 SparkLLM 的 app_id, api_key 和 api_secret,然后设置环境变量 IFLYTEK_SPARK_APP_ID, IFLYTEK_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_core.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])