Skip to content

okcourse.generators.openai

Course generators that use the OpenAI API to produce courses and their assets.

Modules:

Name Description
async_openai

The async_openai module contains the OpenAIAsyncGenerator class.

openai_utils

Shared utilities for interacting with the OpenAI API.

Classes:

Name Description
AIModels
OpenAIAsyncGenerator

Uses the OpenAI API to generate course content asynchronously.

Functions:

Name Description
get_usable_models_async

Asynchronously get the usable models, fetching them if not already cached.

get_usable_models_sync

Synchronously get the usable models using asyncio.run().

AIModels dataclass

AIModels(
    image_models: list[str],
    speech_models: list[str],
    text_models: list[str],
    other_models: list[str] | None,
)

Attributes:

Name Type Description
image_models list[str]
other_models list[str] | None
speech_models list[str]
text_models list[str]

image_models instance-attribute

image_models: list[str]

other_models instance-attribute

other_models: list[str] | None

speech_models instance-attribute

speech_models: list[str]

text_models instance-attribute

text_models: list[str]

OpenAIAsyncGenerator

OpenAIAsyncGenerator(course: Course)

Uses the OpenAI API to generate course content asynchronously.

Use the OpenAIAsyncGenerator to generate a course outline, lectures, cover image, and audio file for a course.

Examples: Generate a full course, including its outline, lectures, cover image, and audio file:

import asyncio
from okcourse import Course, OpenAIAsyncGenerator


async def main() -> None:
    """Use the OpenAIAsyncGenerator to generate a complete course."""

    # Create a course, configure its settings, and initialize the generator
    course = Course(title="From AGI to ASI: Paperclips, Gray Goo, and You")
    generator = OpenAIAsyncGenerator(course)

    # Generate all course content with - these call AI provider APIs
    course = await generator.generate_outline(course)
    course = await generator.generate_lectures(course)
    course = await generator.generate_image(course)
    course = await generator.generate_audio(course)

    # A Course is a Pydantic model, as are its nested models
    print(course.model_dump_json(indent=2))


if __name__ == "__main__":
    asyncio.run(main())

Parameters:

Name Type Description Default
course Course

The course to generate content for.

required

Methods:

Name Description
generate_audio

Generates an audio file from the combined text of the lectures in the given course using a TTS AI model.

generate_course

Generates a complete course, including its outline, lectures, a cover image, and audio.

generate_image

Generates cover art for the course with the given outline.

generate_lectures

Generates the text for the lectures in the course outline in the settings.

generate_outline

Generates a course outline based on its title and other settings.

Attributes:

Name Type Description
client

client instance-attribute

client = AsyncOpenAI()

generate_audio async

generate_audio(course: Course) -> Course

Generates an audio file from the combined text of the lectures in the given course using a TTS AI model.

Returns:

Type Description
Course

The course with its audio_file_path attribute set which points to the TTS-generated file.

generate_course async

generate_course(course: Course) -> Course

Generates a complete course, including its outline, lectures, a cover image, and audio.

Parameters:

Name Type Description Default
course Course

The course to generate.

required

Returns:

Name Type Description
Course Course

The course with all attributes populated by the generation process.

generate_image async

generate_image(course: Course) -> Course

Generates cover art for the course with the given outline.

The image is appropriate for use as cover art for the course text or audio.

Returns:

Type Description
Course

The results of the generation process with the image_bytes attribute set.

Raises:

Type Description
OpenAIError

If an error occurs during image generation.

generate_lectures async

generate_lectures(course: Course) -> Course

Generates the text for the lectures in the course outline in the settings.

To generate an audio file for the Course generated by this method, call generate_audio.

Returns:

Type Description
Course

The Course with its course.lectures attribute set.

generate_outline async

generate_outline(course: Course) -> Course

Generates a course outline based on its title and other settings.

Set the course's title attribute before calling this method.

Returns:

Name Type Description
Course Course

The result of the generation process with its course.outline attribute set.

Raises:

Type Description
ValueError

If the course has no title.

Examples:

    course = Course(title="From AGI to ASI: Paperclips, Gray Goo, and You")
    generator = OpenAIAsyncGenerator(course)
    course = await generator.generate_outline(course)

get_usable_models_async async

get_usable_models_async() -> AIModels

Asynchronously get the usable models, fetching them if not already cached.

get_usable_models_sync

get_usable_models_sync() -> AIModels

Synchronously get the usable models using asyncio.run().