|
24 | 24 |
|
25 | 25 |
|
26 | 26 | class SparkLLM(LLM):
|
27 |
| - """iFlyTek Spark large language model. |
| 27 | + """iFlyTek Spark completion model integration. |
| 28 | +
|
| 29 | + Setup: |
| 30 | + To use, you should set environment variables ``IFLYTEK_SPARK_APP_ID``, |
| 31 | + ``IFLYTEK_SPARK_API_KEY`` and ``IFLYTEK_SPARK_API_SECRET``. |
| 32 | +
|
| 33 | + .. code-block:: bash |
| 34 | +
|
| 35 | + export IFLYTEK_SPARK_APP_ID="your-app-id" |
| 36 | + export IFLYTEK_SPARK_API_KEY="your-api-key" |
| 37 | + export IFLYTEK_SPARK_API_SECRET="your-api-secret" |
| 38 | +
|
| 39 | + Key init args — completion params: |
| 40 | + model: Optional[str] |
| 41 | + Name of IFLYTEK SPARK model to use. |
| 42 | + temperature: Optional[float] |
| 43 | + Sampling temperature. |
| 44 | + top_k: Optional[float] |
| 45 | + What search sampling control to use. |
| 46 | + streaming: Optional[bool] |
| 47 | + Whether to stream the results or not. |
| 48 | +
|
| 49 | + Key init args — client params: |
| 50 | + app_id: Optional[str] |
| 51 | + IFLYTEK SPARK API KEY. Automatically inferred from env var `IFLYTEK_SPARK_APP_ID` if not provided. |
| 52 | + api_key: Optional[str] |
| 53 | + IFLYTEK SPARK API KEY. If not passed in will be read from env var IFLYTEK_SPARK_API_KEY. |
| 54 | + api_secret: Optional[str] |
| 55 | + IFLYTEK SPARK API SECRET. If not passed in will be read from env var IFLYTEK_SPARK_API_SECRET. |
| 56 | + api_url: Optional[str] |
| 57 | + Base URL for API requests. |
| 58 | + timeout: Optional[int] |
| 59 | + Timeout for requests. |
| 60 | +
|
| 61 | + See full list of supported init args and their descriptions in the params section. |
| 62 | +
|
| 63 | + Instantiate: |
| 64 | + .. code-block:: python |
| 65 | +
|
| 66 | + from langchain_community.llms import SparkLLM |
28 | 67 |
|
29 |
| - To use, you should pass `app_id`, `api_key`, `api_secret` |
30 |
| - as a named parameter to the constructor OR set environment |
31 |
| - variables ``IFLYTEK_SPARK_APP_ID``, ``IFLYTEK_SPARK_API_KEY`` and |
32 |
| - ``IFLYTEK_SPARK_API_SECRET`` |
| 68 | + llm = SparkLLM( |
| 69 | + app_id="your-app-id", |
| 70 | + api_key="your-api_key", |
| 71 | + api_secret="your-api-secret", |
| 72 | + # model='Spark4.0 Ultra', |
| 73 | + # temperature=..., |
| 74 | + # other params... |
| 75 | + ) |
33 | 76 |
|
34 |
| - Example: |
| 77 | + Invoke: |
35 | 78 | .. code-block:: python
|
36 | 79 |
|
37 |
| - client = SparkLLM( |
38 |
| - spark_app_id="<app_id>", |
39 |
| - spark_api_key="<api_key>", |
40 |
| - spark_api_secret="<api_secret>" |
41 |
| - ) |
42 |
| - """ |
| 80 | + input_text = "用50个字左右阐述,生命的意义在于" |
| 81 | + llm.invoke(input_text) |
| 82 | +
|
| 83 | + .. code-block:: python |
| 84 | +
|
| 85 | + '生命的意义在于实现自我价值,追求内心的平静与快乐,同时为他人和社会带来正面影响。' |
| 86 | +
|
| 87 | + Stream: |
| 88 | + .. code-block:: python |
| 89 | +
|
| 90 | + for chunk in llm.stream(input_text): |
| 91 | + print(chunk) |
| 92 | +
|
| 93 | + .. code-block:: python |
| 94 | +
|
| 95 | + 生命 | 的意义在于 | 不断探索和 | 实现个人潜能,通过 | 学习 | 、成长和对社会 | 的贡献,追求内心的满足和幸福。 |
| 96 | +
|
| 97 | + Async: |
| 98 | + .. code-block:: python |
| 99 | +
|
| 100 | + await llm.ainvoke(input_text) |
| 101 | +
|
| 102 | + # stream: |
| 103 | + # async for chunk in llm.astream(input_text): |
| 104 | + # print(chunk) |
| 105 | +
|
| 106 | + # batch: |
| 107 | + # await llm.abatch([input_text]) |
| 108 | +
|
| 109 | + .. code-block:: python |
| 110 | +
|
| 111 | + '生命的意义在于实现自我价值,追求内心的平静与快乐,同时为他人和社会带来正面影响。' |
| 112 | +
|
| 113 | + """ # noqa: E501 |
43 | 114 |
|
44 | 115 | client: Any = None #: :meta private:
|
45 |
| - spark_app_id: Optional[str] = None |
46 |
| - spark_api_key: Optional[str] = None |
47 |
| - spark_api_secret: Optional[str] = None |
48 |
| - spark_api_url: Optional[str] = None |
49 |
| - spark_llm_domain: Optional[str] = None |
| 116 | + spark_app_id: Optional[str] = Field(default=None, alias="app_id") |
| 117 | + """Automatically inferred from env var `IFLYTEK_SPARK_APP_ID` |
| 118 | + if not provided.""" |
| 119 | + spark_api_key: Optional[str] = Field(default=None, alias="api_key") |
| 120 | + """IFLYTEK SPARK API KEY. If not passed in will be read from |
| 121 | + env var IFLYTEK_SPARK_API_KEY.""" |
| 122 | + spark_api_secret: Optional[str] = Field(default=None, alias="api_secret") |
| 123 | + """IFLYTEK SPARK API SECRET. If not passed in will be read from |
| 124 | + env var IFLYTEK_SPARK_API_SECRET.""" |
| 125 | + spark_api_url: Optional[str] = Field(default=None, alias="api_url") |
| 126 | + """Base URL path for API requests, leave blank if not using a proxy or service |
| 127 | + emulator.""" |
| 128 | + spark_llm_domain: Optional[str] = Field(default=None, alias="model") |
| 129 | + """Model name to use.""" |
50 | 130 | spark_user_id: str = "lc_user"
|
51 | 131 | streaming: bool = False
|
52 |
| - request_timeout: int = 30 |
| 132 | + """Whether to stream the results or not.""" |
| 133 | + request_timeout: int = Field(default=30, alias="timeout") |
| 134 | + """request timeout for chat http requests""" |
53 | 135 | temperature: float = 0.5
|
| 136 | + """What sampling temperature to use.""" |
54 | 137 | top_k: int = 4
|
| 138 | + """What search sampling control to use.""" |
55 | 139 | model_kwargs: Dict[str, Any] = Field(default_factory=dict)
|
| 140 | + """Holds any model parameters valid for API call not explicitly specified.""" |
56 | 141 |
|
57 | 142 | @pre_init
|
58 | 143 | def validate_environment(cls, values: Dict) -> Dict:
|
59 | 144 | values["spark_app_id"] = get_from_dict_or_env(
|
60 | 145 | values,
|
61 |
| - "spark_app_id", |
| 146 | + ["spark_app_id", "app_id"], |
62 | 147 | "IFLYTEK_SPARK_APP_ID",
|
63 | 148 | )
|
64 | 149 | values["spark_api_key"] = get_from_dict_or_env(
|
65 | 150 | values,
|
66 |
| - "spark_api_key", |
| 151 | + ["spark_api_key", "api_key"], |
67 | 152 | "IFLYTEK_SPARK_API_KEY",
|
68 | 153 | )
|
69 | 154 | values["spark_api_secret"] = get_from_dict_or_env(
|
70 | 155 | values,
|
71 |
| - "spark_api_secret", |
| 156 | + ["spark_api_secret", "api_secret"], |
72 | 157 | "IFLYTEK_SPARK_API_SECRET",
|
73 | 158 | )
|
74 | 159 | values["spark_api_url"] = get_from_dict_or_env(
|
75 | 160 | values,
|
76 |
| - "spark_api_url", |
| 161 | + ["spark_api_url", "api_url"], |
77 | 162 | "IFLYTEK_SPARK_API_URL",
|
78 |
| - "wss://spark-api.xf-yun.com/v3.1/chat", |
| 163 | + "wss://spark-api.xf-yun.com/v3.5/chat", |
79 | 164 | )
|
80 | 165 | values["spark_llm_domain"] = get_from_dict_or_env(
|
81 | 166 | values,
|
82 |
| - "spark_llm_domain", |
| 167 | + ["spark_llm_domain", "model"], |
83 | 168 | "IFLYTEK_SPARK_LLM_DOMAIN",
|
84 |
| - "generalv3", |
| 169 | + "generalv3.5", |
85 | 170 | )
|
86 | 171 | # put extra params into model_kwargs
|
87 | 172 | values["model_kwargs"]["temperature"] = values["temperature"] or cls.temperature
|
@@ -163,7 +248,7 @@ def _stream(
|
163 | 248 | [{"role": "user", "content": prompt}],
|
164 | 249 | self.spark_user_id,
|
165 | 250 | self.model_kwargs,
|
166 |
| - self.streaming, |
| 251 | + True, |
167 | 252 | )
|
168 | 253 | for content in self.client.subscribe(timeout=self.request_timeout):
|
169 | 254 | if "data" not in content:
|
@@ -200,11 +285,11 @@ def __init__(
|
200 | 285 | )
|
201 | 286 |
|
202 | 287 | self.api_url = (
|
203 |
| - "wss://spark-api.xf-yun.com/v3.1/chat" if not api_url else api_url |
| 288 | + "wss://spark-api.xf-yun.com/v3.5/chat" if not api_url else api_url |
204 | 289 | )
|
205 | 290 | self.app_id = app_id
|
206 | 291 | self.model_kwargs = model_kwargs
|
207 |
| - self.spark_domain = spark_domain or "generalv3" |
| 292 | + self.spark_domain = spark_domain or "generalv3.5" |
208 | 293 | self.queue: Queue[Dict] = Queue()
|
209 | 294 | self.blocking_message = {"content": "", "role": "assistant"}
|
210 | 295 | self.api_key = api_key
|
|
0 commit comments