-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmain.py
85 lines (69 loc) · 2.06 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from typing import Any, Dict
from fastapi import Body, FastAPI, Form, Request
from fastapi.responses import HTMLResponse
from pydantic import BaseModel, Field
from dotenv import load_dotenv
import pinecone
import os
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Pinecone
load_dotenv()
pinecone.init(
api_key=os.getenv("PINECONE_API_KEY"),
environment="gcp-starter",
)
embeddings = OpenAIEmbeddings()
vector_store = Pinecone.from_existing_index(
"recipes",
embeddings,
)
app = FastAPI(
title="CheftGPT. The best provider of Indian Recipes in the world.",
description="Give ChefGPT the name of an ingredient and it will give you multiple recipes to use that ingredient on in return.",
servers=[
{
"url": "https://occupations-partition-governments-analyzed.trycloudflare.com",
},
],
)
class Document(BaseModel):
page_content: str
@app.get(
"/recipes",
summary="Returns a list of recipes.",
description="Upon receiving an ingredient, this endpoint will return a list of recipes that contain that ingredient.",
response_description="A Document object that contains the recipe and preparation instructions",
response_model=list[Document],
openapi_extra={
"x-openai-isConsequential": False,
},
)
def get_recipe(ingredient: str):
docs = vector_store.similarity_search(ingredient)
return docs
user_token_db = {"ABCDEF": "nico"}
@app.get(
"/authorize",
response_class=HTMLResponse,
include_in_schema=False,
)
def handle_authorize(client_id: str, redirect_uri: str, state: str):
return f"""
<html>
<head>
<title>Nicolacus Maximus Log In</title>
</head>
<body>
<h1>Log Into Nicolacus Maximus</h1>
<a href="{redirect_uri}?code=ABCDEF&state={state}">Authorize Nicolacus Maximus GPT</a>
</body>
</html>
"""
@app.post(
"/token",
include_in_schema=False,
)
def handle_token(code=Form(...)):
return {
"access_token": user_token_db[code],
}