community[patch]: Update the default “API URL” and “MODEL” of sparkllm (#22070)

- **Description:** When I was running the sparkllm, I found that the
default parameters currently used could no longer run correctly.
    - original parameters & values:
         - spark_api_url: "wss://spark-api.xf-yun.com/v3.1/chat"
         - spark_llm_domain: "generalv3"
    ```python
    # example
    
    from langchain_community.chat_models import ChatSparkLLM
    
spark = ChatSparkLLM(spark_app_id="my_app_id",
spark_api_key="my_api_key", spark_api_secret="my_api_secret")
    spark.invoke("hello")
    ```

![sparkllm](https://github.com/langchain-ai/langchain/assets/55082429/5369bfdf-4305-496a-bcf5-2d3f59d39414)

So I updated them to 3.5 (same as sparkllm official website). After the
update, they can be used normally.
    - new parameters & values:
         - spark_api_url: "wss://spark-api.xf-yun.com/v3.5/chat"
         - spark_llm_domain: "generalv3.5"
This commit is contained in:
maang-h 2024-05-24 00:25:20 +08:00 committed by GitHub
parent 4fda7bf4f2
commit 9aba9e3e33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -43,6 +43,9 @@ from langchain_core.utils import (
logger = logging.getLogger(__name__)
SPARK_API_URL = "wss://spark-api.xf-yun.com/v3.5/chat"
SPARK_LLM_DOMAIN = "generalv3.5"
def _convert_message_to_dict(message: BaseMessage) -> dict:
if isinstance(message, ChatMessage):
@ -108,7 +111,7 @@ class ChatSparkLLM(BaseChatModel):
Extra infos:
1. Get app_id, api_key, api_secret from the iFlyTek Open Platform Console:
https://console.xfyun.cn/services/bm35
2. By default, iFlyTek Spark LLM V3.0 is invoked.
2. By default, iFlyTek Spark LLM V3.5 is invoked.
If you need to invoke other versions, please configure the corresponding
parameters(spark_api_url and spark_llm_domain) according to the document:
https://www.xfyun.cn/doc/spark/Web.html
@ -134,17 +137,31 @@ class ChatSparkLLM(BaseChatModel):
}
client: Any = None #: :meta private:
spark_app_id: Optional[str] = None
spark_app_id: Optional[str] = Field(default=None, alias="app_id")
"""Automatically inferred from env var `IFLYTEK_SPARK_APP_ID`
if not provided."""
spark_api_key: Optional[str] = Field(default=None, alias="api_key")
spark_api_secret: Optional[str] = None
spark_api_url: Optional[str] = None
spark_llm_domain: Optional[str] = None
"""Automatically inferred from env var `IFLYTEK_SPARK_API_KEY`
if not provided."""
spark_api_secret: Optional[str] = Field(default=None, alias="api_secret")
"""Automatically inferred from env var `IFLYTEK_SPARK_API_SECRET`
if not provided."""
spark_api_url: Optional[str] = Field(default=None, alias="api_url")
"""Base URL path for API requests, leave blank if not using a proxy or service
emulator."""
spark_llm_domain: Optional[str] = Field(default=None, alias="model")
"""Model name to use."""
spark_user_id: str = "lc_user"
streaming: bool = False
"""Whether to stream the results or not."""
request_timeout: int = Field(30, alias="timeout")
"""request timeout for chat http requests"""
temperature: float = Field(default=0.5)
"""What sampling temperature to use."""
top_k: int = 4
"""What search sampling control to use."""
model_kwargs: Dict[str, Any] = Field(default_factory=dict)
"""Holds any model parameters valid for API call not explicitly specified."""
class Config:
"""Configuration for this pydantic object."""
@ -199,13 +216,13 @@ class ChatSparkLLM(BaseChatModel):
values,
"spark_api_url",
"IFLYTEK_SPARK_API_URL",
"wss://spark-api.xf-yun.com/v3.1/chat",
SPARK_API_URL,
)
values["spark_llm_domain"] = get_from_dict_or_env(
values,
"spark_llm_domain",
"IFLYTEK_SPARK_LLM_DOMAIN",
"generalv3",
SPARK_LLM_DOMAIN,
)
# put extra params into model_kwargs
values["model_kwargs"]["temperature"] = values["temperature"] or cls.temperature
@ -307,12 +324,10 @@ class _SparkLLMClient:
"Please install it with `pip install websocket-client`."
)
self.api_url = (
"wss://spark-api.xf-yun.com/v3.1/chat" if not api_url else api_url
)
self.api_url = SPARK_API_URL if not api_url else api_url
self.app_id = app_id
self.model_kwargs = model_kwargs
self.spark_domain = spark_domain or "generalv3"
self.spark_domain = spark_domain or SPARK_LLM_DOMAIN
self.queue: Queue[Dict] = Queue()
self.blocking_message = {"content": "", "role": "assistant"}
self.api_key = api_key