Last active
March 26, 2023 17:51
-
-
Save darrenwiens/5dad86385fa4aeefa3ba23843ffa7b26 to your computer and use it in GitHub Desktop.
A FastAPI that makes a request to OpenAI for LeafletJS map code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI | |
from pydantic import BaseModel | |
import openai | |
from fastapi.middleware.cors import CORSMiddleware | |
app = FastAPI() | |
origins = [ | |
"http://localhost:8001", # your allowed origin | |
] | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=origins, | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
OPENAI_API_KEY = "YOUR_OPENAI_API_KEY" | |
class Prompt(BaseModel): | |
prompt: str | |
@app.post("/prompt/") | |
async def create_item(prompt: Prompt): | |
openai.api_key = OPENAI_API_KEY | |
request = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant."}, | |
{ | |
"role": "user", | |
"content": ( | |
f"Show the javascript necessary to create a leafletjs map that {prompt}" | |
f"Show only the javascript." | |
f"The tile layer in the map should use open streetmap, not mapbox." | |
f"The map div element is called 'map'." | |
f"There should be no comments in the code." | |
), | |
} | |
], | |
) | |
# this part needs work | |
try: | |
msg = request.choices[0].message.content.split("```")[1].strip("javascript") | |
except: | |
msg = request.choices[0].message.content | |
return msg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment