Skip to content

Asaray #40

Description

@shadaball-sudo

from fastapi import FastAPI
from pydantic import BaseModel
import openai
import os

app = FastAPI()

Load API key from environment variable

openai.api_key = os.getenv("OPENAI_API_KEY")

class Message(BaseModel):
user_message: str

conversation_history = []

@app.post("/chat")
async def chat(msg: Message):
conversation_history.append({"role": "user", "content": msg.user_message})
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful AI assistant named Asare."},
*conversation_history
]
)
ai_reply = response.choices[0].message['content']
conversation_history.append({"role": "assistant", "content": ai_reply})
return {"reply": ai_reply}

@app.post("/generate-image")
async def generate_image(prompt: Message):
response = openai.Image.create(
prompt=prompt.user_message,
n=1,
size="512x512"
)
image_url = response['data'][0]['url']
return {"image_url": image_url}

@app.get("/")
async def home():
return {"message": "Welcome! Asare is ready to assist you."}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions