Skip to main content

A Python package for interacting with models

Project description

TextxGen

TextxGen is a Python package that provides a seamless interface to interact with Large Language models . It supports chat-based conversations and text completions using predefined models. The package is designed to be simple, modular, and easy to use, making it ideal for developers who want to integrate llm models into their applications.


Features

  • Predefined API Key: No need to provide your own API key—TextxGen uses a predefined key internally.
  • Chat and Completions: Supports both chat-based conversations and text completions.
  • System Prompts: Add system-level prompts to guide model interactions.
  • Error Handling: Robust exception handling for API failures, invalid inputs, and network issues.
  • Modular Design: Easily extendable to support additional models in the future.

Installation

You can install TextxGen in one of two ways:

Option 1: Install via pip

pip install textxgen

Option 2: Clone the Repository

  1. Clone the repository from GitHub:
    git clone https://github.com/Sohail-Shaikh-07/textxgen.git
    
  2. Navigate to the project directory:
    cd textxgen
    
  3. Install the package locally:
    pip install .
    

Usage

1. Chat Example

Use the ChatEndpoint to interact with chat-based models.

from textxgen import ChatEndpoint

def main():
    # Initialize the ChatEndpoint
    chat = ChatEndpoint()

    # Define the conversation messages
    messages = [
        {"role": "user", "content": "What is the capital of France?"},
    ]

    # Add a system prompt (optional)
    system_prompt = "You are a helpful assistant."

    # Send the chat request
    response = chat.chat(
        messages=messages,
        model="llama3",  # Use the LLaMA 3 model
        system_prompt=system_prompt,
        temperature=0.7,  # Adjust creativity
        max_tokens=100,   # Limit response length
    )

    # Format and print the response
    print("=== Chat Response ===")
    print(response["choices"][0]["message"]["content"])

if __name__ == "__main__":
    main()

Output:

=== Chat Response ===
The capital of France is Paris.

2. Completions Example

Use the CompletionsEndpoint to generate text completions.

from textxgen import CompletionsEndpoint

def main():
    # Initialize the CompletionsEndpoint
    completions = CompletionsEndpoint()

    # Define the input prompt
    prompt = "Once upon a time"

    # Send the completion request
    response = completions.complete(
        prompt=prompt,
        model="phi3",      # Use the Phi-3 model
        temperature=0.7,   # Adjust creativity
        max_tokens=100,    # Limit response length
    )

    # Format and print the response
    print("=== Completion Response ===")
    print(response["choices"][0]["text"])

if __name__ == "__main__":
    main()

Output:

=== Completion Response ===
Once upon a time, in a land far, far away...

3. Listing Supported Models

Use the ModelsEndpoint to list and retrieve supported models.

from textxgen import ModelsEndpoint

def main():
    """
    Example usage of the ModelsEndpoint to list and retrieve supported models.
    """
    # Initialize the ModelsEndpoint
    models = ModelsEndpoint()

    # List all supported models
    print("=== Supported Models ===")
    for model_name, display_name in models.list_display_models().items():
        print(f"{model_name}: {display_name}")


if __name__ == "__main__":
    main()

Output:

=== Supported Models ===
llama3: LLaMA 3 (8B Instruct)
phi3: Phi-3 Mini (128K Instruct)
deepseek: DeepSeek Chat
qwen2_5: Qwen 2.5 (3B Parameters)
deepseek_v3: Deepseek Chat V3
gemma3_4b: Google Gemma 3 (4B Parameters)
gemma3_1b: Google Gemma 3 (1B Parameters)
qwen3: Qwen 3 (14B Parameters)
deepseek_r1_t: Deepseek R1-T (Chimera)
deepcoder_14b: Deepcoder (14B Parameters)
llama4: Llama 4 Maverick (17B Instruct)
qwerky_72b: Qwerky (72B Parameters)
huggingface_4: HuggingFace Zephyr (7B Parameters Beta)
gpt4_1_nano: OpenAI GPT 4.1 Nano
gpt4o_mini: OpenAI GPT 4o Mini

4. Streaming Example

You can enable streaming for real-time responses by setting stream=True.

Chat Streaming Example

from textxgen import ChatEndpoint

# Initialize the ChatEndpoint
chat = ChatEndpoint()

# Define the conversation messages
messages = [
    {"role": "user", "content": "What is the capital of France?"},
]

# Add a system prompt (optional)
system_prompt = "You are a helpful assistant."

# Send the chat request with streaming
print("=== Chat Response (Streaming) ===")
response_stream = chat.chat(
    messages=messages,
    model="llama3",
    system_prompt=system_prompt,
    temperature=0.7,
    max_tokens=100,
    stream=True,  # Enable streaming
)

# Process the streaming response
for chunk in response_stream:
    if "choices" in chunk and len(chunk["choices"]) > 0:
        content = chunk["choices"][0].get("delta", {}).get("content", "")
        if content:
            print(content, end="", flush=True)

print("\n")

Output:

The capital of France is Paris.

Chat Completion Example

from textxgen import CompletionsEndpoint

# Initialize the CompletionsEndpoint
completions = CompletionsEndpoint()

# Define the input prompt
prompt = "Once upon a time"

# Send the completion request with streaming
print("=== Completion Response (Streaming) ===")
response_stream = completions.complete(
    prompt=prompt,
    model="phi3",
    temperature=0.7,
    max_tokens=100,
    stream=True,  # Enable streaming
)

# Process the streaming response
for chunk in response_stream:
    if "choices" in chunk and len(chunk["choices"]) > 0:
        # Extract the generated text from the response
        text = chunk["choices"][0].get("text", "")
        if text:
            print(text, end="", flush=True)

print("\n")

Output:

Once upon a time, in a land far, far away...

Supported Models

TextxGen currently supports the following models:

Model Name Model ID Description
LLaMA 3 (8B Instruct) llama3 A powerful 8-billion parameter model for general-purpose tasks.
Phi-3 Mini (128K Instruct) phi3 A lightweight yet capable model optimized for efficiency.
DeepSeek Chat deepseek A conversational model designed for interactive chat.
Qwen 2.5 (3B Parameters) qwen2_5 A versatile 3B parameter model with vision-language capabilities.
DeepSeek Chat V3 deepseek_v3 The latest version of DeepSeek's conversational model.
Google Gemma 3 (4B Parameters) gemma3_4b Google's 4B parameter model optimized for various tasks.
Google Gemma 3 (1B Parameters) gemma3_1b A lightweight 1B parameter version of Google's Gemma model.
Qwen 3 (14B Parameters) qwen3 A powerful 14B parameter model for advanced tasks.
DeepSeek R1-T (Chimera) deepseek_r1_t A specialized model from DeepSeek's R1 series.
Deepcoder (14B Parameters) deepcoder_14b A 14B parameter model focused on coding tasks.
Llama 4 Maverick (17B Instruct) llama4 Meta's latest 17B parameter model for instruction following.
Qwerky (72B Parameters) qwerky_72b A massive 72B parameter model for complex tasks.
HuggingFace Zephyr (7B Parameters Beta) huggingface_4 HuggingFace's 7B parameter model in beta testing.
OpenAI GPT 4.1 Nano gpt4_1_nano OpenAI's compact version of GPT 4.1.
OpenAI GPT 4o Mini gpt4o_mini OpenAI's mini version of GPT 4o.

Error Handling

TextxGen provides robust error handling for common issues:

  • Invalid Input: Raised when invalid input is provided (e.g., empty messages or prompts).
  • API Errors: Raised when the API returns an error (e.g., network issues or invalid requests).
  • Unsupported Models: Raised when an unsupported model is requested.

Example:

from textxgen.exceptions import InvalidInputError

try:
    response = chat.chat(messages=[])
except InvalidInputError as e:
    print("Error:", str(e))

Contributing

Contributions are welcome! To contribute to TextxGen:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Submit a pull request with a detailed description of your changes.

License

TextxGen is licensed under the MIT License. See the LICENSE file for details.


Buy Me a Coffee

If you find TextxGen useful and would like to support its development, you can buy me a coffee! Your support helps maintain and improve the project.

Buy Me A Coffee


Support

If you encounter any issues or have questions, please open an issue on the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textxgen-1.0.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textxgen-1.0.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file textxgen-1.0.0.tar.gz.

File metadata

  • Download URL: textxgen-1.0.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for textxgen-1.0.0.tar.gz
Algorithm Hash digest
SHA256 67d25f421c384ea0a6e9ea5612cec7f3500ea38646c8b7f1701e27547d3d9d89
MD5 f96d11af5aed1babc8fab6c02dcb8fbc
BLAKE2b-256 815ac474e767f2d5300ab9b7eca30a048be6df93f0bf394bb2ec9f0ead3c882b

See more details on using hashes here.

File details

Details for the file textxgen-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: textxgen-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for textxgen-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c757a7ea805918c8e1846e244d216efdb33713f19a6f5bdf1f0e9b49f50dcd3b
MD5 0257b6053ed59689bced10947cfef262
BLAKE2b-256 32f678e6e0f9e00d3fbfdb3d812c77bd69e6d7d0fbd46bf5094d339908adfc10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page