Skip to main content

A simple llm library.

Project description

llmtext

alt text

llmtext is a simple yet powerful library designed to interact with large language models (LLMs) as straightforward functions. It provides easy-to-use interfaces for both input-output text transformations and input-to-Pydantic class conversions, leveraging the power of open-source LLMs and OpenAI's schema.

Features

  • Input Text, Output Text: Seamlessly generate text outputs from text inputs using large language models.
  • Input Text, Output Pydantic Class: Convert text inputs directly into structured Pydantic classes for better data validation and manipulation.
  • OpenAI Schema Support: Utilize OpenAI's schema for consistent and robust text processing.
  • OpenSource LLMs through TogetherAI: Access a variety of open-source LLMs via TogetherAI for flexible and cost-effective solutions.
  • Async by default: Asynchronous by default
  • Robusts: configured with retry and self healing loop for structured extraction

Installation

You can install llmtext via pip:

pip install llmtext

Usage

Text to Text Transformation

To generate text outputs from text inputs:

from llmtext.text import Text

text = Text(text="What is the capital of France ?")
res = await llm.arun(text="What is the capital of France?")

Text to Pydantic Class Transformation

To convert text inputs into a Pydantic class:

from llmtext.text import Text

text = Text(text="The city of France is Paris. It's a beautiful city.")

class ExtractedData(BaseModel):
    name: Annotated[str, Field(description="Name of the city")]
    description: Annotated[str, Field(description="Description of the city")]

res = await text.astructured_extraction(output_class=ExtractedData)
assert isinstance(res, ExtractedData)

Text to Streaming Pydantic Class Transformation

To convert text inputs into a Pydantic class:

from llmtext.text import Text

text = Text(text="The city of France is Paris. It's a beautiful city.")

class ExtractedData(BaseModel):
    name: Annotated[str, Field(description="Name of the city")]
    description: Annotated[str, Field(description="Description of the city")]

stream = await text.astream_structured_extraction(output_class=ExtractedData)

async for res in stream:
    assert isinstance(res, ExtractedData)

Agents

from llmtext.agent import Agent
from llmtext.chat import Chat
from llmtext.tools import RunnableTool

class SearchTool(RunnableTool):
    """Use this tool to search for news articles."""

    query: Annotated[str, Field(description="search query")]

    async def arun(self) -> str:
        return f"there's no result for: {self.query}, please try again"

class CalculateTool(RunnableTool):
    """Use this tool to calculate the sum of two numbers."""

    a: int
    b: int

    async def arun(self) -> int:
        return self.a + self.b

class MultiplyTool(RunnableTool):
    """Use this tool to multiply 2 numbers"""

    a: int
    b: int

    async def arun(self) -> int:
        return self.a * self.b

agent = Agent(
    chat=Chat(
        messages=[
            {
                "role": "user",
                "content": "what's 1000 + 2000 + 3000 + 4000 ? After that multiple result by 6",
            }
        ]
    ),
    tools=[SearchTool, CalculateTool, MultiplyTool],
)

stream = agent.astream_events()

async for chunk in stream:
    print(chunk)
    print("\n")

Configuration

To configure llmtext for using OpenAI's schema or TogetherAI's open-source LLMs, you can set the necessary API keys in your environment variables or configuration file.

Example Configuration to use togetherai, or openrouter or any other open-source LLMs that support openai schema

OPENAI_API_KEY=
OPENAI_BASE_URL=https://api.together.xyz/v1
OPENAI_MODEL=

or input it upon class initialization

llm = Text(
    text="What is the capital of France ?",
    openai_client=AsyncOpenAI(
        api_key=os.getenv("OPENROUTER_API_KEY", ""),
        base_url=os.getenv("OPENROUTER_BASE_URL"),
    ),
    openai_model="anthropic/claude-3-haiku",
)

Contributing

We welcome contributions to llmtext. Please fork the repository and submit pull requests. For major changes, please open an issue first to discuss what you would like to change.

License

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

Acknowledgments

Special thanks to OpenAI for providing robust schema support and TogetherAI for enabling access to open-source LLMs.


For more information, please refer to the documentation.

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

llmtext-4.1.5.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

llmtext-4.1.5-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file llmtext-4.1.5.tar.gz.

File metadata

  • Download URL: llmtext-4.1.5.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-38-generic

File hashes

Hashes for llmtext-4.1.5.tar.gz
Algorithm Hash digest
SHA256 632f3b39cad5ff18220f45f3b80cbb12d83fc3ac9a9edf200b552cce0ce1fa81
MD5 488c5d1984c8dce537f34043d07ea9e0
BLAKE2b-256 23df51d51195ba7e386b55b5e99e59d7f8873beaba20f4422cceb06063f67180

See more details on using hashes here.

File details

Details for the file llmtext-4.1.5-py3-none-any.whl.

File metadata

  • Download URL: llmtext-4.1.5-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-38-generic

File hashes

Hashes for llmtext-4.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3c3536e82338039c9a4446021197494a5ebea2964e9e3197037c3e7e894d92ee
MD5 d9151afec1e5ef43b9e224dcde8c5c8f
BLAKE2b-256 4160aca8b0db56ad212540a80f34ea67d52b946bccbea7242e6c7e37f820629b

See more details on using hashes here.

Supported by

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