跳到主要內容
Open In ColabOpen on GitHub

SparkLLM 聊天

iFlyTek 提供的 SparkLLM 聊天模型 API。更多資訊請參閱 iFlyTek 開放平台

基本用法

"""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])
API 參考文件:ChatSparkLLM | HumanMessage
AIMessage(content='Hello! How can I help you today?')
  • iFlyTek SparkLLM API Console 取得 SparkLLM 的 app_id、api_key 和 api_secret(更多資訊請參閱 iFlyTek 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_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])
API 參考文件:ChatSparkLLM | HumanMessage

這個頁面有幫助嗎?