Skip to content

Latest commit

 

History

History
101 lines (75 loc) · 2.59 KB

File metadata and controls

101 lines (75 loc) · 2.59 KB
title Quickstart: Backend
description How to setup the CopilotBackend endpoints

Setup

Setup the CopilotKit Backend Endpoint:

To sign up to the Copilot Cloud waitlist click here. For now you must host your own Backend, which is straightforward:

Install CopilotKit Backend packages

Install the CopilotKit backend packagess:

```bash npm npm i @copilotkit/backend ``` ```bash yarn yarn add @copilotkit/backend ``` ```bash pnpm pnpm add @copilotkit/backend ```

Manually specified endpoint: Next.js edge function (typescript)

Remember to add any required environment variables. E.g. to use OpenAI:

OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
import { CopilotBackend, OpenAIAdapter } from "@copilotkit/backend";

export const runtime = "edge";

export async function POST(req: Request): Promise<Response> {
  const copilotKit = new CopilotBackend();

  return copilotKit.response(req, new OpenAIAdapter());
}

Manually specified endpoint: Node.js (typescript)

Remember to add any required environment variables. E.g. to use OpenAI:

OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
import http from "http";
import { CopilotBackend, OpenAIAdapter } from "@copilotkit/backend";

const HEADERS = {
  // make sure to modify CORS headers to match your frontend's origin
  "Access-Control-Allow-Origin": "http://localhost:3000",
  "Access-Control-Allow-Methods": "POST, OPTIONS",
  "Access-Control-Allow-Headers": "Content-Type",
};

const server = http.createServer((request, response) => {
  try {
    const headers = {
      ...HEADERS,
      ...(request.method === "POST" && { "Content-Type": "application/json" }),
    };
    response.writeHead(200, headers);
    if (request.method == "POST") {
      const copilotKit = new CopilotBackend();
      const openaiAdapter = new OpenAIAdapter();
      copilotKit.streamHttpServerResponse(request, response, openaiAdapter);
    } else {
      response.end("openai server");
    }
  } catch (err) {
    console.error(err);
    response.end("error");
  }
});

const port = 4201;
const host = "localhost";
server.listen(port, host);
console.log(`Listening at http://${host}:${port}`);

Firebase (GCP) Cloud Function (typescript)

See this repo for a full example: https://github.com/CopilotKit/copilotkit-firebase-demo