generative-ts

Function createOpenAiChatModelProvider

  • Creates a OpenAI ModelProvider with the OpenAiChatApi

    import { createOpenAiChatModelProvider } from "generative-ts";

    const gpt = createOpenAiChatModelProvider({
    modelId: "gpt-4-turbo", // OpenAI defined model ID
    // you can explicitly pass auth here, otherwise by default it is read from process.env
    });

    const response = await gpt.sendRequest({
    $prompt: "Brief History of NY Mets:",
    max_tokens: 100,
    // all other OpenAI ChatCompletion options available here
    });

    console.log(response.choices[0]?.message.content);

    Provider Setup and Notes

    Create an API account at OpenAI

    Obtain a OpenAI API key and either pass it explicitly in auth or set it in the environment as OPENAI_API_KEY

    Model Parameters

    Model IDs

    Type Parameters

    • THttpClientOptions = HttpClientOptions

    Parameters

    • params: {
          auth?: OpenAiAuthConfig;
          client?: HttpClient<THttpClientOptions>;
          modelId: string;
      }
      • Optional auth?: OpenAiAuthConfig

        Authentication configuration for OpenAI. If not supplied, it will be loaded from the environment.

      • Optional client?: HttpClient<THttpClientOptions>

        HTTP client to use for requests. If not supplied, the built-in fetch-based implementation will be used.

      • modelId: string

        The model ID as defined by OpenAI.

    Returns HttpModelProvider<OpenAiChatOptions, OpenAiChatResponse, THttpClientOptions, {
        modelId: string;
    }>

    The OpenAI Model Provider with the OpenAiChatApi

    See

    Throws

    If no auth is passed and OPENAI_API_KEY is not found in process.env

    Example: Usage

    import { createOpenAiChatModelProvider } from "generative-ts";

    const gpt = createOpenAiChatModelProvider({
    modelId: "gpt-4-turbo", // OpenAI defined model ID
    // you can explicitly pass auth here, otherwise by default it is read from process.env
    });

    const response = await gpt.sendRequest({
    $prompt: "Brief History of NY Mets:",
    max_tokens: 100,
    // all other OpenAI ChatCompletion options available here
    });

    console.log(response.choices[0]?.message.content);