Skip to main content
Open In Colab在 GitHub 上打开

SparkLLM 聊天

科大讯飞的 SparkLLM 聊天模型 API。更多信息,请参见 科大讯飞 Open Platform

基本用途

"""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 控制台获取 SparkLLM 的 app_id、api_key 和 api_secret(更多信息,请参见 iFlyTek SparkLLM Intro),然后设置环境变量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])