The py/genai_client/text_generation/textgen_client.py module provides the TextGenClient class, which is designed to interact with a Text Generation WebUI (often referred to as Oobabooga) server. This server typically hosts open-source language models and exposes an API for text generation.
- Purpose: This class acts as a client for language models served via Oobabooga's Text Generation WebUI. It provides a standardized interface within SEMOSS to send prompts and receive generated text from these models, abstracting the direct API calls to the WebUI.
- Relationship to Framework: It extends
AbstractTextGenerationClient, inheriting functionalities like prompt template management and the publicask()method. The core generation logic is implemented in itsask_call()method.
The TextGenClient is initialized with the following parameters:
endpoint(str): Required. The base URL of the Text Generation WebUI server (e.g.,http://localhost:5000).template(Optional[Union[Dict, str]]): A dictionary of prompt templates or a path to a JSON file containing templates.model_name(Optional[str]): The name or identifier of the model being served by the Text Generation WebUI. This is primarily used to initialize aHuggingfaceTokenizerfor client-side token counting and truncation.template_name(Optional[str]): The default prompt template name to use.stop_sequences(Optional[List[str]], default:[]): A list of strings that, if generated, will cause the model to stop generating further text. If empty, it defaults to the tokenizer'seos_tokenif available.timeout(Optional[int], default:30): Timeout in seconds for API requests to the WebUI.**kwargs: Additional keyword arguments.MAX_TOKENS,MAX_INPUT_TOKENS: Passed toHuggingfaceTokenizerfor setting token limits.
The constructor initializes a text_generation.Client instance (from the text-generation library by Hugging Face, which is a common client for TGI and similar backends) using the provided endpoint. It also sets up a HuggingfaceTokenizer based on the model_name.
-
ask_call(self, question: str = None, context: Optional[str] = None, ..., **kwargs) -> AskModelEngineResponse:- This is the primary method for making requests to the Text Generation WebUI.
- Prompt Construction:
- If
FULL_PROMPTis inkwargs, it uses this as the complete prompt (can be a string or a list of role/content dicts which it formats into a single string). - Otherwise, it uses
_fill_params()and_process_history()to construct the prompt string fromquestion,context,template_name,history, and otherkwargs(for template filling). _fill_params(): Fills templates usingself.fill_context()orself.fill_template()._process_history(): Formats chat history into the prompt string, typically in a "Role: Content\n\n" format. It tries to place history before the main content if a template substitution occurred, otherwise it appends history and then the question.
- If
- Token Limit Handling: Calls
_check_token_limits()to truncate the prompt if it exceeds model/tokenizer limits and adjustmax_new_tokensaccordingly. - API Parameters: Prepares a
parametersdictionary for theself.client.generate()orself.client.generate_stream()call. This includes:prompt(str): The fully constructed prompt.do_sample(bool, default:False)max_new_tokens(int, default:1000)repetition_penalty(Optional[float])return_full_text(bool, default:False)seed(Optional[int])stop_sequences(List[str])temperature(Optional[float])top_k(Optional[int])top_p(Optional[float])truncate(Optional[int]): Note: Client-side truncation is also performed by_check_token_limits.typical_p(Optional[float])watermark(bool, default:False)
- API Call:
- If
streamisTrue(passed inkwargs, defaults toTrue), it callsself.client.generate_stream(**parameters). It iterates through the streamed response tokens, concatenates them, and prints them with theprefix. - If
streamisFalse, it callsself.client.generate(**parameters).
- If
- Logprobs & Token Details: If
decoder_input_details=Trueis passed (for non-streaming) orinclude_logprobs=True(for streaming, though TGI streaming often doesn't provide detailed token logprobs per chunk in the same way as a non-streaming detailed response), it attempts to capture token texts and log probabilities. - Response Packaging: Returns an
AskModelEngineResponsecontaining:response: The generated text.prompt_tokens: Calculated by_check_token_limits.response_tokens: Calculated by counting the tokens in the generated text (for streaming) or fromresponse.details.tokens(for non-streaming if details are available).tokensandlogprobslists if requested and available.
-
_process_full_prompt(self, full_prompt: Union[List, str]) -> str:- Formats a
full_prompt(either a list of OpenAI-style message dicts or a raw string) into a single string suitable for the Text Generation WebUI.
- Formats a
-
_check_token_limits(self, prompt_payload: str, max_new_tokens: int) -> Tuple[str, int, AskModelEngineResponse]:- Uses the initialized
HuggingfaceTokenizerto count tokens inprompt_payload. - Truncates
prompt_payloadfrom the beginning if it exceedsmax_input_tokens(ormax_tokensas a fallback from tokenizer settings). - Adjusts
max_new_tokensto ensureprompt_tokens + max_new_tokensdoes not exceed the overallmax_tokenslimit of the tokenizer, applying a 5% buffer. - Populates
prompt_tokensand anywarningin anAskModelEngineResponseobject.
- Uses the initialized
- The client uses the
text_generation.Clientlibrary, which is designed to interact with Hugging Face's Text Generation Inference (TGI) toolkit. Oobabooga's Text Generation WebUI often exposes a TGI-compatible API. - Endpoints Used: The
text_generation.Clienttypically interacts with endpoints like:/generate(for non-streaming)/generate_stream(for streaming)
- Payload Structure: The JSON payload sent to these endpoints includes parameters like
prompt,do_sample,max_new_tokens,temperature,top_p,top_k,stop_sequences, etc., as detailed in theparametersdictionary withinask_call.
- Client-Side Tokenization & Truncation: Proactively checks and truncates prompt length based on the configured tokenizer and model limits before sending the request.
- Flexible Prompt Construction: Offers multiple ways to build the final prompt: using
question/context, leveraging prompt templates, or providing aFULL_PROMPT. - Streaming Support: Capable of handling streamed responses from the API.
- Parameter Mapping: Maps common LLM parameters to the specific names expected by the
text_generation.Clientlibrary.
The TextGenClient enables SEMOSS to utilize a wide variety of open-source models hosted via the Text Generation WebUI by providing a compatible client interface.