Skip to content

OpenAIReader

Overview

The OpenAIReader class is designed to interface with OpenAI's GPT-3 models to extract relationships between entities in a given text. It extends the BaseLLMReader class and uses the openai library to communicate with the API.

Installation

  1. Install the openai library:
    pip install openai
    

Example Usage

Here's an example demonstrating how to use the OpenAIReader class:

from euler.llm_readers import OpenAIReader

# Initialize the OpenAIReader
openai_reader = OpenAIReader(
    api_key='your_openai_api_key_here',
    model_id='text-davinci-003'
)

# Read text and extract relationships
text = """Thomas Alva Edison was an American inventor and businessman. He developed many devices in fields such as electric power generation, mass communication, sound recording, and motion pictures."""
response = openai_reader.read(text)
print(response.text if response else "No response received.")

Using LLMReaderFactory

The LLMReaderFactory class can be used to easily initialize different LLM readers, including OpenAIReader.

from euler.llm_readers import LLMReaderFactory

# Initialize the OpenAIReader using LLMReaderFactory
openai_reader = LLMReaderFactory.get_llm_reader(
    reader_type='openai',
    api_key='your_openai_api_key_here',
    model_id='text-davinci-003'
)

# Read text and extract relationships
text = """Thomas Alva Edison was an American inventor and businessman. He developed many devices in fields such as electric power generation, mass communication, sound recording, and motion pictures."""
response = openai_reader.read(text)
print(response.text if response else "No response received.")

Classes and Methods

OpenAIConfig

The OpenAIConfig class is a configuration model that holds the API key and model ID.

Attributes: - api_key (str): The API key for accessing the OpenAI API. - model_id (str): The model ID to use for generating responses.

OpenAIResponse

The OpenAIResponse class represents the response from the OpenAI API.

Attributes: - text (str): The response text generated by the OpenAI model.

OpenAIReader

The OpenAIReader class interfaces with OpenAI's GPT-3 models to extract relationships between entities in a given text.

Methods: - __init__(self, api_key: str, model_id: str = 'text-davinci-003'): Initializes the reader with the API key and model ID. - read(self, text: str, custom_prompt: Optional[str] = None) -> Optional[OpenAIResponse]: Reads the text and returns the extracted relationships.

This documentation provides an overview, example usage, and detailed description of the OpenAIReader class and its associated classes and methods.