Skip to content

Commit

Permalink
true_or_false
Browse files Browse the repository at this point in the history
  • Loading branch information
olohmann committed Jun 5, 2024
1 parent f746d75 commit c93ceb0
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions src-agents/phase1/main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import os
from dotenv import load_dotenv
from fastapi import FastAPI, Response, Request
from fastapi import FastAPI
from pydantic import BaseModel
from enum import Enum
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from starlette.background import BackgroundTask
from starlette.types import Message
import logging
logging.basicConfig(filename='info.log', level=logging.INFO)

app = FastAPI()

load_dotenv()

def log_info(req_body, res_body):
logging.info(req_body)
logging.info(res_body)


class QuestionType(str, Enum):
multiple_choice = "multiple_choice"
true_or_false = "true_or_false"
true_false = "true_or_false"
popular_choice = "popular_choice"
estimation = "estimation"

Expand Down Expand Up @@ -54,19 +45,6 @@ class Answer(BaseModel):

deployment_name = os.getenv("AZURE_OPENAI_COMPLETION_DEPLOYMENT_NAME")

@app.middleware('http')
async def logging_middleware(request: Request, call_next):
req_body = await request.body()
response = await call_next(request)

res_body = b''
async for chunk in response.body_iterator:
res_body += chunk

task = BackgroundTask(log_info, req_body, res_body)
return Response(content=res_body, status_code=response.status_code,
headers=dict(response.headers), media_type=response.media_type, background=task)

@app.get("/")
async def root():
return {"message": "Hello Smorgs"}
Expand All @@ -85,7 +63,7 @@ async def ask_question(ask: Ask):

# Send a completion call to generate an answer
print('Sending a request to openai')
start_phrase = "Answer the following question. Do only provide the answer, no additional 'a)' or 'b)' or whatever. Question: " + ask.question
start_phrase = ask.question
response = client.chat.completions.create(
model = deployment_name,
messages = [{"role" : "assistant", "content" : start_phrase}],
Expand All @@ -98,4 +76,4 @@ async def ask_question(ask: Ask):
answer.promptTokensUsed = response.usage.prompt_tokens
answer.completionTokensUsed = response.usage.completion_tokens

return answer
return answer

0 comments on commit c93ceb0

Please sign in to comment.