Skip to content

Watsonx

WatsonX #

Bases: LLM

IBM WatsonX LLM。

示例

pip install llama-index-llms-watsonx

from llama_index.llms.watsonx import WatsonX

credentials = {
    "url": "https://enter.your-ibm.url",
    "apikey": "insert_your_api_key",
}

project_id = "insert_your_project_id"

llm = WatsonX(credentials=credentials, project_id=project_id)

resp = llm.complete("Paul Graham is")
print(resp)
Source code in llama_index/llms/watsonx/base.py
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
class WatsonX(LLM):
    """IBM WatsonX LLM。

    示例:
        `pip install llama-index-llms-watsonx`

        ```python
        from llama_index.llms.watsonx import WatsonX

        credentials = {
            "url": "https://enter.your-ibm.url",
            "apikey": "insert_your_api_key",
        }

        project_id = "insert_your_project_id"

        llm = WatsonX(credentials=credentials, project_id=project_id)

        resp = llm.complete("Paul Graham is")
        print(resp)
        ```"""

    model_id: str = Field(description="The Model to use.")
    max_new_tokens: int = Field(description="The maximum number of tokens to generate.")
    temperature: float = Field(description="The temperature to use for sampling.")
    additional_kwargs: Dict[str, Any] = Field(
        default_factory=dict, description="Additional Kwargs for the WatsonX model"
    )
    model_info: Dict[str, Any] = Field(
        default_factory=dict, description="Details about the selected model"
    )

    _model = PrivateAttr()

    def __init__(
        self,
        credentials: Dict[str, Any],
        model_id: Optional[str] = "ibm/granite-13b-chat-v2",
        project_id: Optional[str] = None,
        space_id: Optional[str] = None,
        max_new_tokens: Optional[int] = 512,
        temperature: Optional[float] = 0.1,
        additional_kwargs: Optional[Dict[str, Any]] = None,
        callback_manager: Optional[CallbackManager] = None,
        system_prompt: Optional[str] = None,
        messages_to_prompt: Optional[Callable[[Sequence[ChatMessage]], str]] = None,
        completion_to_prompt: Optional[Callable[[str], str]] = None,
        pydantic_program_mode: PydanticProgramMode = PydanticProgramMode.DEFAULT,
        output_parser: Optional[BaseOutputParser] = None,
    ) -> None:
        """初始化参数。"""
        if model_id not in WATSONX_MODELS:
            raise ValueError(
                f"Model name {model_id} not found in {WATSONX_MODELS.keys()}"
            )

        additional_kwargs = additional_kwargs or {}
        callback_manager = callback_manager or CallbackManager([])

        project_id = get_from_param_or_env_without_error(
            project_id, "IBM_WATSONX_PROJECT_ID"
        )
        space_id = get_from_param_or_env_without_error(space_id, "IBM_WATSONX_SPACE_ID")

        if project_id is not None or space_id is not None:
            self._model = Model(
                model_id=model_id,
                credentials=credentials,
                project_id=project_id,
                space_id=space_id,
            )
        else:
            raise ValueError(
                "Did not find `project_id` or `space_id`, Please pass them as named parameters"
                " or as environment variables, `IBM_WATSONX_PROJECT_ID` or `IBM_WATSONX_SPACE_ID`."
            )

        super().__init__(
            model_id=model_id,
            temperature=temperature,
            max_new_tokens=max_new_tokens,
            additional_kwargs=additional_kwargs,
            model_info=self._model.get_details(),
            callback_manager=callback_manager,
            system_prompt=system_prompt,
            messages_to_prompt=messages_to_prompt,
            completion_to_prompt=completion_to_prompt,
            pydantic_program_mode=pydantic_program_mode,
            output_parser=output_parser,
        )

    @classmethod
    def class_name(self) -> str:
        """获取类名。"""
        return "WatsonX_LLM"

    @property
    def metadata(self) -> LLMMetadata:
        return LLMMetadata(
            context_window=watsonx_model_to_context_size(self.model_id),
            num_output=self.max_new_tokens,
            model_name=self.model_id,
        )

    @property
    def sample_model_kwargs(self) -> Dict[str, Any]:
        """获取用户可以传递给模型的模型kwargs的示例。"""
        try:
            from ibm_watson_machine_learning.metanames import GenTextParamsMetaNames
        except ImportError as e:
            raise ImportError(
                "You must install the `ibm_watson_machine_learning` package to use WatsonX"
                "please `pip install ibm_watson_machine_learning`"
            ) from e

        params = GenTextParamsMetaNames().get_example_values()

        params.pop("return_options")

        return params

    @property
    def _model_kwargs(self) -> Dict[str, Any]:
        base_kwargs = {
            "max_new_tokens": self.max_new_tokens,
            "temperature": self.temperature,
        }

        return {**base_kwargs, **self.additional_kwargs}

    def _get_all_kwargs(self, **kwargs: Any) -> Dict[str, Any]:
        return {**self._model_kwargs, **kwargs}

    @llm_completion_callback()
    def complete(
        self, prompt: str, formatted: bool = False, **kwargs: Any
    ) -> CompletionResponse:
        all_kwargs = self._get_all_kwargs(**kwargs)

        response = self._model.generate_text(prompt=prompt, params=all_kwargs)

        return CompletionResponse(text=response)

    @llm_completion_callback()
    def stream_complete(
        self, prompt: str, formatted: bool = False, **kwargs: Any
    ) -> CompletionResponseGen:
        all_kwargs = self._get_all_kwargs(**kwargs)

        stream_response = self._model.generate_text_stream(
            prompt=prompt, params=all_kwargs
        )

        def gen() -> CompletionResponseGen:
            content = ""
            for stream_delta in stream_response:
                content += stream_delta
                yield CompletionResponse(text=content, delta=stream_delta)

        return gen()

    @llm_chat_callback()
    def chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
        all_kwargs = self._get_all_kwargs(**kwargs)
        chat_fn = completion_to_chat_decorator(self.complete)

        return chat_fn(messages, **all_kwargs)

    @llm_chat_callback()
    def stream_chat(
        self, messages: Sequence[ChatMessage], **kwargs: Any
    ) -> ChatResponseGen:
        all_kwargs = self._get_all_kwargs(**kwargs)
        chat_stream_fn = stream_completion_to_chat_decorator(self.stream_complete)

        return chat_stream_fn(messages, **all_kwargs)

    # Async Functions
    # IBM Watson Machine Learning Package currently does not have Support for Async calls

    async def acomplete(
        self, prompt: str, formatted: bool = False, **kwargs: Any
    ) -> CompletionResponse:
        raise NotImplementedError

    async def astream_chat(
        self, messages: Sequence[ChatMessage], **kwargs: Any
    ) -> ChatResponseAsyncGen:
        raise NotImplementedError

    async def achat(
        self, messages: Sequence[ChatMessage], **kwargs: Any
    ) -> ChatResponse:
        raise NotImplementedError

    async def astream_complete(
        self, prompt: str, formatted: bool = False, **kwargs: Any
    ) -> CompletionResponseAsyncGen:
        raise NotImplementedError

sample_model_kwargs property #

sample_model_kwargs: Dict[str, Any]

获取用户可以传递给模型的模型kwargs的示例。

class_name classmethod #

class_name() -> str

获取类名。

Source code in llama_index/llms/watsonx/base.py
121
122
123
124
@classmethod
def class_name(self) -> str:
    """获取类名。"""
    return "WatsonX_LLM"