Skip to content

chigwell/text-to-struct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

text-to-struct

PyPI version License: MIT Downloads LinkedIn

A Python package that transforms unstructured text inputs into structured, standardized outputs using LLM7 and llmatch-messages.

Overview

text_to_struct enables users to process textual descriptions, ideas, or concepts, and generate structured data adhering to a predefined format. It leverages the LLM7 language model via the langchain_llm7 package, combined with llmatch-messages to ensure the output matches specified patterns. This approach promotes consistency and ease of parsing, making the package particularly useful for content creation, brainstorming, and idea management systems.

Installation

pip install text_to_struct

Usage

from text_to_struct import text_to_struct

# Example usage with default LLM7
response = text_to_struct(user_input="Describe a new innovative app idea.")
print(response)

Parameters

  • user_input (str): The unstructured text to be processed.
  • llm (Optional[BaseChatModel]): An optional langchain LLM instance. If not provided, the default ChatLLM7 will be used.
  • api_key (Optional[str]): The API key for LLM7. If not provided, it will be fetched from the LLM7_API_KEY environment variable.

Available LLMs

You can pass your own LLM instances from various providers, such as:

Using langchain's OpenAI

from langchain_openai import ChatOpenAI
from text_to_struct import text_to_struct

llm = ChatOpenAI()
response = text_to_struct(user_input, llm=llm)

Using langchain's Anthropic

from langchain_anthropic import ChatAnthropic
from text_to_struct import text_to_struct

llm = ChatAnthropic()
response = text_to_struct(user_input, llm=llm)

Using langchain's Google Generative AI

from langchain_google_genai import ChatGoogleGenerativeAI
from text_to_struct import text_to_struct

llm = ChatGoogleGenerativeAI()
response = text_to_struct(user_input, llm=llm)

API Key & Rate Limits

The default free-tier rate limits for LLM7 are sufficient for most use cases. To access higher rate limits, set your API key via:

  • Environment variable: LLM7_API_KEY
  • Or pass directly: text_to_struct(user_input, api_key="your_api_key")

You can obtain a free API key at https://token.llm7.io/.

Development & Support

Source Code

The core functionality is built on the langchain_llm7 package (available on PyPI), which interfaces with LLM7. The package also uses llmatch_messages for pattern matching and ensuring output structure.


Note: The text_to_struct function relies on a pattern matching regex and specific prompts (system_prompt and human_prompt) to guide the LLM output. Make sure your input and patterns align with your expected structured output.