Skip to content
Adding a Chatgpt Prompt Endpoint

Adding a ChatGPT prompt endpoint to Embedbase

This example demonstrates how to add a ChatGPT prompt endpoint to Embedbase that returns a random prompt from the awesome-chatgpt-prompts (opens in a new tab) dataset that you can use.

Installation

Install the required dependencies in a virtual environment:

virtualenv env
source env/bin/activate
pip install embedbase

Start Embedbase

Create a new file main.py with the following code:

main.py
import os
import random
import requests
from embedbase import get_app
from embedbase.database.memory_db import MemoryDatabase
from embedbase.embedding.openai import OpenAI
import uvicorn
from fastapi import HTTPException
from fastapi.responses import JSONResponse
 
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
 
app = (
    get_app()
    .use_embedder(OpenAI(os.environ["OPENAI_API_KEY"]))
    .use_db(MemoryDatabase())
    .run()
)
 
# An endpoint returning random chatgpt prompts
@app.get("/prompts")
async def get_prompts():
    url = "https://datasets-server.huggingface.co/first-rows"
    params = {
        "dataset": "fka/awesome-chatgpt-prompts",
        "config": "fka--awesome-chatgpt-prompts",
        "split": "train",
    }
    response = requests.get(url, params=params)
    if response.status_code == 200:
        data = response.json()
        rows = data["rows"]
        random_prompt = random.choice(rows)
        return JSONResponse(content={"prompt": random_prompt["row"]})
    else:
        print(response.status_code)
        print(response.text)
        raise HTTPException(status_code=500, detail="Error fetching prompts")
 
if __name__ == "__main__":
    uvicorn.run("main:app", reload=True)

Start the Embedbase application with the following command:

python3 main.py

Test the endpoint

curl localhost:8000/prompts
{
    "prompt": {
        "act": "Aphorism Book",
        "prompt": "I want you to act as an aphorism book. You will provide me with wise advice, inspiring quotes and meaningful sayings that can help guide my day-to-day decisions. Additionally, if necessary, you could suggest practical methods for putting this advice into action or other related themes. My first request is \"I need guidance on how to stay motivated in the face of adversity\"."
    }
}