Socket
Socket
Sign inDemoInstall

chatgpt-cloudflare

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    chatgpt-cloudflare

ChatGPT API client, compatible with Cloudflare Workers


Version published
Maintainers
1
Install size
7.70 kB
Created

Readme

Source

chatgpt-cloudflare

ChatGPT API client, compatible with Cloudflare Workers

Running tests

In order to execute tests that make call to real ChatGPT, please copy dev.vars.default into .dev.vars and replace key with the real one.

Usage example

import { ChatGPTClient, ConversationContext } from "chatgpt-cloudflare";

export interface Env {
  OPENAI_API_KEY: string;
}

export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext
  ): Promise<Response> {
    const openAiClient = new ChatGPTClient(env.OPENAI_API_KEY);
    const body = (await request.json()) as Record<string, any>;

    const context = new ConversationContext();
    await openAiClient.getResponse(
      {
        prompt:
          "After this sentence, I will give you some text; please repeat all of it verbatim, but add an exclamation point in its end. \n " +
          body.prompt,
        systemContext: "You are a helpful assistant.",
      },
      context
    );

    const response2 = await openAiClient.getResponse(
      {
        prompt:
          "Please respond with your previous answer, but add one more exclamation mark to it",
      },
      context
    );

    const responseText = response2.text;

    return new Response(
      JSON.stringify({
        response: responseText,
      })
    );
  },
};

Keywords

FAQs

Last updated on 26 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc