Skip to content

GCP HTTP Function to communicate with custom OpenAI GPT Assistant

License

Notifications You must be signed in to change notification settings

entroped/gpt-cloud-function

Repository files navigation

GPT Cloud Function

build

GCP HTTP Function to communicate with custom OpenAI GPT Assistant. This repository provides an example to create an assistant with knowledge base, faq and general information about its identity. It is perfectly suitable to use as a Customer Support.

Getting Started

Retrieving OpenAI API Key

  • Go to Openai API Keys page and create an API key for this project
    • Permissions should be write to Assistants, Threads, and Files.
  • Add API key (2 options)
    OPENAI_SECRET_NAME=projects/0020090084006/secrets/OPENAI_API_KEY
    • Manually: Put API key into .env file
    OPENAI_API_KEY=Your API KEY

Local Testing

Use your own package manager to install and test locally with the start npm command. (pnpm recommended)

npm install
npm run build
npm run start

Cloud Testing

Install

  • Install Google Cloud CLI (known as Cloud CDK)
    • gcloud init
  • Create a project and set it as your default project
    • gcloud config set project YOUR_PROJECT_ID

Deploy

npm install
npm run build
npm run gcp-deploy

Using the API

You can use the function at http://localhost:8080/ locally and/or with your Cloud Function Public URL via HTTP Post methods.

Steps

Initially you will need to send an starting message to the Assistant with the following format:

{
   "content": "Tell me about Quick Chat API"
}

The answer will have the threadId, status, runId and the result array with the message thread.

{
   "result": [
      {
         "id": "string",
         "object": "string",
         "created_at": 0,
         "assistant_id": "string",
         "thread_id": "string",
         "run_id": "string",
         "role": "assistant",
         "content": [
            {
               "type": "text",
               "text": {
                  "value": "Hi, I am happy to help.",
                  "annotations": []
               }
            },
            {
               "type": "text",
               "text": {
                  "value": "Tell me about Quick Chat API",
                  "annotations": []
               }
            }
         ],
         "attachments": [],
         "metadata": {}
      }
   ],
   "threadId": "string",
   "status": "string",
   "runId": "string"
}

Extract the threadId, and use it in the next message from now on to continue the conversation.

{
"content": "This is my second question",
"threadId": "{{threadId}}"
}

Contribute

There are many ways to contribute to GPT Cloud Functions.

About

GCP HTTP Function to communicate with custom OpenAI GPT Assistant

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published