Skip to content

Sambanova

SambaStudio #

Bases: CustomLLM

SambaStudio LLM.

Examples:

pip install llama-index-llms-sambanova

from llama_index.llms.sambanova import SambaStudio

llm = Sambaverse(...)

response = llm.complete("What is the meaning of life?")

print(response)
Source code in llama-index-integrations/llms/llama-index-llms-sambanova/llama_index/llms/sambanova/base.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
class SambaStudio(CustomLLM):
    """
    SambaStudio LLM.

    Examples:
        `pip install llama-index-llms-sambanova`

        ```python
        from llama_index.llms.sambanova import SambaStudio

        llm = Sambaverse(...)

        response = llm.complete("What is the meaning of life?")

        print(response)
        ```
    """

    sambastudio_base_url: str = Field(
        default="",
        description="URL of the SambaStudio server",
    )
    sambastudio_base_uri: str = Field(
        default="",
        description="Base URI of the SambaStudio server",
    )
    sambastudio_project_id: str = Field(
        default="",
        description="Project ID of the SambaStudio server",
    )
    sambastudio_endpoint_id: str = Field(
        default="",
        description="Endpoint ID of the SambaStudio server",
    )
    sambastudio_api_key: str = Field(
        default="",
        description="API key for the SambaStudio server",
    )

    model_kwargs: Dict[str, Any] = Field(
        default_factory=dict,
        description="Additional keyword arguments to pass to the model",
    )
    streaming: bool = Field(
        default=False,
        description="Boolean to state whether to stream response or not",
    )

    _client: requests.Session = PrivateAttr()

    def __init__(
        self,
        sambastudio_base_url: str = "",
        sambastudio_base_uri: str = "",
        sambastudio_project_id: str = "",
        sambastudio_endpoint_id: str = "",
        model_kwargs: Dict[str, Any] = {},
        streaming: bool = False,
        client: Optional[requests.Session] = None,
    ) -> None:
        super().__init__()

        self.sambastudio_base_url = sambastudio_base_url
        self.sambastudio_base_uri = sambastudio_base_uri
        self.sambastudio_project_id = sambastudio_project_id
        self.sambastudio_endpoint_id = sambastudio_endpoint_id
        self.model_kwargs = model_kwargs
        self.streaming = streaming
        self._client = client or requests.Session()
        self._validate_env_vars()

    def _validate_env_vars(self):
        if not self.sambaverse_api_key:
            self.sambaverse_api_key = os.getenv("SAMBAVERSE_API_KEY")
        if not self.sambastudio_base_url:
            self.sambastudio_base_url = os.getenv("SAMBASTUDIO_BASE_URL")
        if not self.sambastudio_base_uri:
            self.sambastudio_base_uri = os.getenv("SAMBASTUDIO_BASE_URI")
        if not self.sambastudio_project_id:
            self.sambastudio_project_id = os.getenv("SAMBASTUDIO_PROJECT_ID")
        if not self.sambastudio_endpoint_id:
            self.sambastudio_endpoint_id = os.getenv("SAMBASTUDIO_ENDPOINT_ID")

        if not self.sambaverse_api_key:
            raise ValueError(
                "Sambaverse API key must be provided either as an argument or set in the environment variable 'SAMBAVERSE_API_KEY'."
            )

        if not self.sambastudio_base_url:
            raise ValueError(
                "Sambastudio base URL must be provided either as an argument or set in the environment variable 'SAMBASTUDIO_BASE_URL'."
            )

        if not self.sambastudio_base_uri:
            raise ValueError(
                "Sambastudio base URI must be provided either as an argument or set in the environment variable 'SAMBASTUDIO_BASE_URI'."
            )

        if not self.sambastudio_project_id:
            raise ValueError(
                "Sambastudio project ID must be provided either as an argument or set in the environment variable 'SAMBASTUDIO_PROJECT_ID'."
            )

        if not self.sambastudio_endpoint_id:
            raise ValueError(
                "Sambastudio endpoint ID must be provided either as an argument or set in the environment variable 'SAMBASTUDIO_ENDPOINT_ID'."
            )

    def _get_full_url(self, path: str) -> str:
        return f"{self.sambastudio_base_url}/{self.sambastudio_base_uri}/{path}"

    def _get_model_kwargs(self, stop: Optional[List[str]]) -> str:
        try:
            _model_kwargs = self.model_kwargs or {}
            _kwarg_stop_sequences = set(_model_kwargs.get("stop_sequences", []))
            _stop_sequences = set(stop or _kwarg_stop_sequences)

            if not _kwarg_stop_sequences:
                _model_kwargs["stop_sequences"] = ",".join(
                    f'"{x}"' for x in _stop_sequences
                )

            tuning_params_dict = {
                k: {"type": type(v).__name__, "value": str(v)}
                for k, v in _model_kwargs.items()
            }

            return json.dumps(tuning_params_dict)

        except Exception as e:
            raise ValueError(f"Error getting model kwargs: {e}")

    def _process_api_response(self, response: requests.Response) -> Dict:
        result: Dict[str, Any] = {}
        try:
            result = response.json()
        except Exception as e:
            result["detail"] = str(e)
        if "status_code" not in result:
            result["status_code"] = response.status_code
        return result

    def _process_api_stream_response(self, response: requests.Response) -> Any:
        """Process the streaming response."""
        if "nlp" in self.sambastudio_base_uri:
            try:
                import sseclient
            except ImportError:
                raise ImportError(
                    "could not import sseclient library"
                    "Please install it with `pip install sseclient-py`."
                )
            client = sseclient.SSEClient(response)
            close_conn = False
            for event in client.events():
                if event.event == "error_event":
                    close_conn = True
                chunk = {
                    "event": event.event,
                    "data": event.data,
                    "status_code": response.status_code,
                }
                yield chunk
            if close_conn:
                client.close()
        elif "generic" in self.sambastudio_base_uri:
            try:
                for line in response.iter_lines():
                    chunk = json.loads(line)
                    if "status_code" not in chunk:
                        chunk["status_code"] = response.status_code
                    if chunk["status_code"] == 200 and chunk.get("error"):
                        chunk["result"] = {"responses": [{"stream_token": ""}]}
                    yield chunk
            except Exception as e:
                raise RuntimeError(f"Error processing streaming response: {e}")
        else:
            raise ValueError(
                f"handling of endpoint uri: {self.api_base_uri} not implemented"
            )

    def _send_sambaverse_request(
        self, data: Dict[str, Any], stream: bool = False
    ) -> requests.Response:
        try:
            if stream:
                url = self._get_full_url(
                    f"stream/{self.sambastudio_project_id}/{self.sambastudio_endpoint_id}"
                )
                headers = {
                    "key": self.sambaverse_api_key,
                    "Content-Type": "application/json",
                }
                return self._client.post(url, headers=headers, json=data, stream=True)
            else:
                url = self._get_full_url(
                    f"{self.sambastudio_project_id}/{self.sambastudio_endpoint_id}"
                )
                headers = {
                    "key": self.sambaverse_api_key,
                    "Content-Type": "application/json",
                }

                return self._client.post(url, headers=headers, json=data, stream=stream)
        except Exception as e:
            raise ValueError(f"Error sending request to Sambaverse: {e}")

    def _prepare_request_data(self, prompt: str) -> Dict[str, Any]:
        try:
            data = {}
            if isinstance(prompt, str):
                input = [prompt]
            if "nlp" in self.api_base_uri:
                model_params = self._get_model_kwargs(stop=None)
                if model_params:
                    data = {"inputs": input, "params": json.loads(model_params)}
                else:
                    data = {"inputs": input}
            elif "generic" in self.api_base_uri:
                model_params = self._get_model_kwargs(stop=None)
                if model_params:
                    data = {"instance": input, "params": json.loads(model_params)}
                else:
                    data = {"instance": input}
            else:
                raise ValueError(
                    f"handling of endpoint uri: {self.api_base_uri} not implemented"
                )
            return data

        except Exception as e:
            raise ValueError(f"Error preparing request data: {e}")

    def _get_completion_from_response(self, response: Dict) -> str:
        try:
            if "nlp" in self.sambastudio_base_uri:
                return response["data"][0]["completion"]
            elif "generic" in self.sambastudio_base_uri:
                return response["predictions"][0]["completion"]
            else:
                raise ValueError(
                    f"handling of endpoint uri: {self.sambastudio_base_uri} not implemented"
                )
        except Exception as e:
            raise ValueError(f"Error processing response: {e}")

    def _get_stream_token_from_response(self, response: Dict) -> str:
        try:
            if "nlp" in self.sambastudio_base_uri:
                return response["data"]["stream_token"]
            elif "generic" in self.sambastudio_base_uri:
                return response["result"]["responses"][0]["stream_token"]
            else:
                raise ValueError(
                    f"handling of endpoint uri: {self.sambastudio_base_uri} not implemented"
                )
        except Exception as e:
            raise ValueError(f"Error processing response: {e}")

    @classmethod
    def class_name(cls) -> str:
        return "SambaStudio"

    @property
    def metadata(self) -> LLMMetadata:
        """LLM metadata."""
        return LLMMetadata(
            model_kwargs=self.model_kwargs,
            description="Sambanova LLM",
            is_streaming=self.streaming,
        )

    @llm_completion_callback()
    def complete(self, prompt: str) -> CompletionResponse:
        """
        Complete the given prompt using the SambaStudio model.

        Args:
            prompt (str): The input prompt to complete.

        Returns:
            CompletionResponse: The completed text generated by the model.
        """
        data = self._prepare_request_data(prompt)
        response = self._send_sambaverse_request(data)
        processed_response = self._process_api_response(response)
        completion_text = self._get_completion_from_response(processed_response)

        return CompletionResponse(text=completion_text)

    @llm_completion_callback()
    def stream_complete(self, prompt: str) -> CompletionResponseGen:
        """
        Stream the completion of the given prompt using the SambaStudio model.

        Args:
            prompt (str): The input prompt to complete.

        Yields:
            CompletionResponseGen: Streamed completion text generated by the model.
        """
        print("In stream_complete")
        data = self._prepare_request_data(prompt)
        response = self._send_sambaverse_request(data, stream=True)

        for token in self._process_api_stream_response(response):
            processed_token = self._get_stream_token_from_response(token)
            yield CompletionResponse(text=processed_token)

    @llm_completion_callback()
    def nlp_prediction(self, prompt: str) -> CompletionResponse:
        """
        Perform NLP prediction for the given prompt using the SambaStudio model.

        Args:
            prompt (str): The input prompt to predict.

        Returns:
            CompletionResponse: The prediction result generated by the model.
        """
        return self.complete(prompt)

    @llm_completion_callback()
    def nlp_prediction_stream(self, prompt: str) -> CompletionResponseGen:
        """
        Stream NLP prediction for the given prompt using the SambaStudio model.

        Args:
            prompt (str): The input prompt to predict.

        Yields:
            CompletionResponseGen: Streamed prediction result generated by the model.
        """
        return self.stream_complete(prompt)

metadata property #

metadata: LLMMetadata

LLM metadata.

complete #

complete(prompt: str) -> CompletionResponse

Complete the given prompt using the SambaStudio model.

Parameters:

Name Type Description Default
prompt str

The input prompt to complete.

required

Returns:

Name Type Description
CompletionResponse CompletionResponse

The completed text generated by the model.

Source code in llama-index-integrations/llms/llama-index-llms-sambanova/llama_index/llms/sambanova/base.py
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
@llm_completion_callback()
def complete(self, prompt: str) -> CompletionResponse:
    """
    Complete the given prompt using the SambaStudio model.

    Args:
        prompt (str): The input prompt to complete.

    Returns:
        CompletionResponse: The completed text generated by the model.
    """
    data = self._prepare_request_data(prompt)
    response = self._send_sambaverse_request(data)
    processed_response = self._process_api_response(response)
    completion_text = self._get_completion_from_response(processed_response)

    return CompletionResponse(text=completion_text)

stream_complete #

stream_complete(prompt: str) -> CompletionResponseGen

Stream the completion of the given prompt using the SambaStudio model.

Parameters:

Name Type Description Default
prompt str

The input prompt to complete.

required

Yields:

Name Type Description
CompletionResponseGen CompletionResponseGen

Streamed completion text generated by the model.

Source code in llama-index-integrations/llms/llama-index-llms-sambanova/llama_index/llms/sambanova/base.py
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
@llm_completion_callback()
def stream_complete(self, prompt: str) -> CompletionResponseGen:
    """
    Stream the completion of the given prompt using the SambaStudio model.

    Args:
        prompt (str): The input prompt to complete.

    Yields:
        CompletionResponseGen: Streamed completion text generated by the model.
    """
    print("In stream_complete")
    data = self._prepare_request_data(prompt)
    response = self._send_sambaverse_request(data, stream=True)

    for token in self._process_api_stream_response(response):
        processed_token = self._get_stream_token_from_response(token)
        yield CompletionResponse(text=processed_token)

nlp_prediction #

nlp_prediction(prompt: str) -> CompletionResponse

Perform NLP prediction for the given prompt using the SambaStudio model.

Parameters:

Name Type Description Default
prompt str

The input prompt to predict.

required

Returns:

Name Type Description
CompletionResponse CompletionResponse

The prediction result generated by the model.

Source code in llama-index-integrations/llms/llama-index-llms-sambanova/llama_index/llms/sambanova/base.py
551
552
553
554
555
556
557
558
559
560
561
562
@llm_completion_callback()
def nlp_prediction(self, prompt: str) -> CompletionResponse:
    """
    Perform NLP prediction for the given prompt using the SambaStudio model.

    Args:
        prompt (str): The input prompt to predict.

    Returns:
        CompletionResponse: The prediction result generated by the model.
    """
    return self.complete(prompt)

nlp_prediction_stream #

nlp_prediction_stream(prompt: str) -> CompletionResponseGen

Stream NLP prediction for the given prompt using the SambaStudio model.

Parameters:

Name Type Description Default
prompt str

The input prompt to predict.

required

Yields:

Name Type Description
CompletionResponseGen CompletionResponseGen

Streamed prediction result generated by the model.

Source code in llama-index-integrations/llms/llama-index-llms-sambanova/llama_index/llms/sambanova/base.py
564
565
566
567
568
569
570
571
572
573
574
575
@llm_completion_callback()
def nlp_prediction_stream(self, prompt: str) -> CompletionResponseGen:
    """
    Stream NLP prediction for the given prompt using the SambaStudio model.

    Args:
        prompt (str): The input prompt to predict.

    Yields:
        CompletionResponseGen: Streamed prediction result generated by the model.
    """
    return self.stream_complete(prompt)

Sambaverse #

Bases: CustomLLM

Sambaverse LLM.

Examples:

pip install llama-index-llms-sambanova

from llama_index.llms.sambanova import Sambaverse

llm = Sambaverse(...)

response = llm.complete("What is the meaning of life?")

print(response)
Source code in llama-index-integrations/llms/llama-index-llms-sambanova/llama_index/llms/sambanova/base.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 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
229
230
231
232
233
234
235
236
237
238
239
class Sambaverse(CustomLLM):
    """
    Sambaverse LLM.

    Examples:
        `pip install llama-index-llms-sambanova`

        ```python
        from llama_index.llms.sambanova import Sambaverse

        llm = Sambaverse(...)

        response = llm.complete("What is the meaning of life?")

        print(response)
        ```
    """

    sambaverse_url: str = Field(
        default="https://sambaverse.sambanova.ai",
        description="URL of the Sambaverse server",
    )

    sambaverse_api_key: str = Field(
        default="",
        description="API key for the Sambaverse server",
    )
    sambaverse_model_name: str = Field(
        default="", description="Name of the Sambaverse model to use"
    )
    model_kwargs: Dict[str, Any] = Field(
        default_factory=dict,
        description="Additional keyword arguments to pass to the model",
    )
    streaming: bool = Field(
        default=False,
        description="Boolean to state whether to stream response or not",
    )

    _client: requests.Session = PrivateAttr()

    def __init__(
        self,
        sambaverse_url: str = "https://sambaverse.sambanova.ai",
        sambaverse_api_key: str = "",
        sambaverse_model_name: str = "",
        model_kwargs: Dict[str, Any] = {},
        streaming: bool = False,
        client: Optional[requests.Session] = None,
    ) -> None:
        super().__init__()

        self.sambaverse_url = sambaverse_url
        self.sambaverse_api_key = sambaverse_api_key
        self.sambaverse_model_name = sambaverse_model_name
        self.model_kwargs = model_kwargs
        self.streaming = streaming
        self._client = client or requests.Session()
        self._validate_env_vars()

    def _validate_env_vars(self):
        if not self.sambaverse_model_name:
            self.sambaverse_model_name = os.getenv("SAMBAVERSE_MODEL_NAME")
        if not self.sambaverse_api_key:
            self.sambaverse_api_key = os.getenv("SAMBAVERSE_API_KEY")

        if not self.sambaverse_model_name:
            raise ValueError(
                "Sambaverse model name must be provided either as an argument or set in the environment variable 'SAMBAVERSE_MODEL_NAME'."
            )

        if not self.sambaverse_api_key:
            raise ValueError(
                "Sambaverse API key must be provided either as an argument or set in the environment variable 'SAMBAVERSE_API_KEY'."
            )

    def _get_full_url(self, endpoint: str) -> str:
        return f"{self.sambaverse_url}/{endpoint}"

    def _get_model_kwargs(self, stop: Optional[List[str]]) -> str:
        try:
            _model_kwargs = self.model_kwargs or {}
            _kwarg_stop_sequences = set(_model_kwargs.get("stop_sequences", []))
            _stop_sequences = set(stop or _kwarg_stop_sequences)

            if not _kwarg_stop_sequences:
                _model_kwargs["stop_sequences"] = ",".join(
                    f'"{x}"' for x in _stop_sequences
                )

            tuning_params_dict = {
                k: {"type": type(v).__name__, "value": str(v)}
                for k, v in _model_kwargs.items()
            }

            return json.dumps(tuning_params_dict)

        except Exception as e:
            raise ValueError(f"Error getting model kwargs: {e}")

    def _process_api_response(self, response: requests.Response) -> Dict:
        result: Dict[str, Any] = {}
        if response.status_code != 200:
            raise ValueError(
                f"Received unexpected status code {response.status_code}: {response.text}"
            )

        try:
            lines_result = response.text.strip().split("\n")
            text_result = lines_result[-1]
            if response.status_code == 200 and json.loads(text_result).get("error"):
                completion = ""
                for line in lines_result[:-1]:
                    completion += json.loads(line)["result"]["responses"][0][
                        "stream_token"
                    ]
                text_result = lines_result[-2]
                result = json.loads(text_result)
                result["result"]["responses"][0]["completion"] = completion
            else:
                result = json.loads(text_result)
        except Exception as e:
            result["detail"] = str(e)
        if "status_code" not in result:
            result["status_code"] = response.status_code
        return result

    def _process_api_stream_response(self, response: requests.Response) -> Any:
        try:
            for line in response.iter_lines():
                chunk = json.loads(line)
                if "status_code" not in chunk:
                    chunk["status_code"] = response.status_code
                if chunk["status_code"] == 200 and chunk.get("error"):
                    chunk["result"] = {"responses": [{"stream_token": ""}]}
                    return chunk
                yield chunk
        except Exception as e:
            raise RuntimeError(f"Error processing streaming response: {e}")

    def _send_sambaverse_request(
        self, endpoint: str, data: Dict[str, Any], stream: bool = False
    ) -> requests.Response:
        url = self._get_full_url(endpoint)
        headers = {
            "key": self.sambaverse_api_key,
            "Content-Type": "application/json",
            "modelName": self.sambaverse_model_name,
        }
        try:
            return self._client.post(url, headers=headers, json=data, stream=stream)

        except Exception as e:
            raise ValueError(f"Error sending request to Sambaverse: {e}")

    def _prepare_request_data(self, prompt: str) -> Dict[str, Any]:
        try:
            model_params = self._get_model_kwargs(stop=None)
            return {"instance": prompt, "params": json.loads(model_params)}

        except Exception as e:
            raise ValueError(f"Error preparing request data: {e}")

    def _get_completion_from_response(self, response: Dict) -> str:
        try:
            return (
                response.get("result", {})
                .get("responses", [{}])[0]
                .get("completion", "")
            )
        except Exception as e:
            raise ValueError(f"Error processing response: {e}")

    @classmethod
    def class_name(cls) -> str:
        return "Samabaverse"

    @property
    def metadata(self) -> LLMMetadata:
        """LLM metadata."""
        return LLMMetadata(
            model_name=self.sambaverse_model_name,
            model_kwargs=self.model_kwargs,
            description="Sambanova LLM",
            is_streaming=self.streaming,
        )

    @llm_completion_callback()
    def complete(self, prompt: str) -> CompletionResponse:
        """
        Complete the given prompt using the Sambaverse model.

        Args:
            prompt (str): The input prompt to complete.

        Returns:
            CompletionResponse: The completed text generated by the model.
        """
        data = self._prepare_request_data(prompt)
        response = self._send_sambaverse_request("api/predict", data)
        processed_response = self._process_api_response(response)
        completion_text = self._get_completion_from_response(processed_response)

        return CompletionResponse(text=completion_text)

    @llm_completion_callback()
    def stream_complete(self, prompt: str) -> CompletionResponseGen:
        """
        Stream the completion of the given prompt using the Sambaverse model.

        Args:
            prompt (str): The input prompt to complete.

        Yields:
            CompletionResponseGen: Streamed completion text generated by the model.
        """
        print("In stream_complete")
        data = self._prepare_request_data(prompt)
        response = self._send_sambaverse_request("api/predict", data, stream=True)

        for token in self._process_api_stream_response(response):
            processed_token = token["result"]["responses"][0]["stream_token"]
            yield CompletionResponse(text=processed_token)

metadata property #

metadata: LLMMetadata

LLM metadata.

complete #

complete(prompt: str) -> CompletionResponse

Complete the given prompt using the Sambaverse model.

Parameters:

Name Type Description Default
prompt str

The input prompt to complete.

required

Returns:

Name Type Description
CompletionResponse CompletionResponse

The completed text generated by the model.

Source code in llama-index-integrations/llms/llama-index-llms-sambanova/llama_index/llms/sambanova/base.py
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
@llm_completion_callback()
def complete(self, prompt: str) -> CompletionResponse:
    """
    Complete the given prompt using the Sambaverse model.

    Args:
        prompt (str): The input prompt to complete.

    Returns:
        CompletionResponse: The completed text generated by the model.
    """
    data = self._prepare_request_data(prompt)
    response = self._send_sambaverse_request("api/predict", data)
    processed_response = self._process_api_response(response)
    completion_text = self._get_completion_from_response(processed_response)

    return CompletionResponse(text=completion_text)

stream_complete #

stream_complete(prompt: str) -> CompletionResponseGen

Stream the completion of the given prompt using the Sambaverse model.

Parameters:

Name Type Description Default
prompt str

The input prompt to complete.

required

Yields:

Name Type Description
CompletionResponseGen CompletionResponseGen

Streamed completion text generated by the model.

Source code in llama-index-integrations/llms/llama-index-llms-sambanova/llama_index/llms/sambanova/base.py
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
@llm_completion_callback()
def stream_complete(self, prompt: str) -> CompletionResponseGen:
    """
    Stream the completion of the given prompt using the Sambaverse model.

    Args:
        prompt (str): The input prompt to complete.

    Yields:
        CompletionResponseGen: Streamed completion text generated by the model.
    """
    print("In stream_complete")
    data = self._prepare_request_data(prompt)
    response = self._send_sambaverse_request("api/predict", data, stream=True)

    for token in self._process_api_stream_response(response):
        processed_token = token["result"]["responses"][0]["stream_token"]
        yield CompletionResponse(text=processed_token)