From 1fc59ecc3fb76268ee267ea940b36045cf370926 Mon Sep 17 00:00:00 2001 From: Tim Walter Date: Wed, 10 Apr 2024 22:27:46 +0200 Subject: [PATCH] =?UTF-8?q?Events=20fertiggestellt;=20Prompts=20ver=C3=A4n?= =?UTF-8?q?dert;=20Function=20Calls=20laufen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API/azure_openai.py | 30 ++++++++++++++++++++++++++++-- API/function_app.py | 17 +++++++++-------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/API/azure_openai.py b/API/azure_openai.py index 9a8868f..a79e8e6 100644 --- a/API/azure_openai.py +++ b/API/azure_openai.py @@ -140,7 +140,7 @@ async def get_or_create_thread(self, user_email: str) -> str: return thread_id - async def chat(self, user_email: str, prompt: str): + async def chat(self, user_name: str, user_email: str, user_prompt: str): """ Sendet den Prompt des Benutzers an den Azure-spezifischen Assistant und verarbeitet die Antwort. @@ -152,6 +152,20 @@ async def chat(self, user_email: str, prompt: str): """ # Methode zu lang. Aufteilung im kommenden Update. + u = UserThreads() + transscript_allowed = await u.get_extended_events(user_email) + + if transscript_allowed == 1: + details = {"email": user_email, "Name: ": user_name,"prompt": user_prompt} + async with EventGridPublisher() as publisher: + await publisher.send_event(event = PromptFromUserEvent(details).to_cloudevent()) + logging.info(f"Prompt von Benutzer {user_email} an EventGrid gesendet.") + await u.close + + # Create Prompt + # Timestamp für Prompt erstellen + time_stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + modified_prompt = f"Mein Name: {user_name}\nDatum und Uhrzeit: {time_stamp}\nMitlesen erlaubt: {transscript_allowed}\nMein Prompt: {user_prompt}" try: thread_id = await self.get_or_create_thread(user_email) @@ -163,8 +177,14 @@ async def chat(self, user_email: str, prompt: str): await self.async_api_call(lambda: self.client.beta.threads.messages.create( thread_id=thread_id, role="user", - content=prompt + content=modified_prompt )) + + details = {"email": user_email, "Name: ": user_name,"prompt": modified_prompt} + async with EventGridPublisher() as publisher: + await publisher.send_event(event = PromptToAIEvent(details).to_cloudevent()) + logging.info(f"Modifiziertes Prompt zum Backend an EventGrid gesendet.") + break except Exception as e: logging.info(f"Error: {e}") @@ -227,6 +247,12 @@ async def chat(self, user_email: str, prompt: str): if messages.data and len(messages.data) > 0 and messages.data[0].content: return_prompt = messages.data[0].content logging.info(f"Response: {return_prompt}") + + details = {"email": user_email, "Name: ": user_name,"ai_prompt": return_prompt} + async with EventGridPublisher() as publisher: + await publisher.send_event(event = PromptFromAIEvent(details).to_cloudevent()) + logging.info(f"Prompt von der AI für Benutzer {user_email} an EventGrid gesendet.") + return 200, return_prompt else: return 200, "Da fällt mir im Moment gerade nichts zu ein (Leere Nachricht von der KI)." diff --git a/API/function_app.py b/API/function_app.py index 8cf4735..2c22582 100644 --- a/API/function_app.py +++ b/API/function_app.py @@ -18,6 +18,8 @@ from datetime import datetime from azure_openai import InteractWithOpenAI import aiohttp +from event_grid_publisher import EventGridPublisher +from my_cloudevents import PromptToUserEvent # Initialisierung der Funktion App @@ -80,12 +82,6 @@ async def chat(req: func.HttpRequest) -> func.HttpResponse: except Exception as e: return func.HttpResponse(f"An error has occured: {e}", status_code=400) - # Timestamp für Prompt erstellen - time_stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - - # Prompt für OpenAI erstellen - prompt = f"Mein Name: {params.user_name}\nDatum und Uhrzeit: {time_stamp}\nMein Prompt: {params.user_prompt}" - # async with InteractWithOpenAI() as interaction: # logging.info(f"Chat-Endpoint: Calling... {params.user_email} mit Prompt: {prompt}") # http_status, response = await interaction.chat(params.user_email, prompt) @@ -93,8 +89,8 @@ async def chat(req: func.HttpRequest) -> func.HttpResponse: interaction = InteractWithOpenAI() try: - logging.info(f"Chat-Endpoint: Calling... {params.user_email} mit Prompt: {prompt}") - http_status, response = await interaction.chat(params.user_email, prompt) + logging.info(f"Chat-Endpoint: Calling... {params.user_email} mit Prompt: {params.user_prompt}") + http_status, response = await interaction.chat(params.user_name, params.user_email, params.user_prompt) # logging.info(f"Chat-Endpoint came back: Response: {http_status}: {response}") finally: await interaction.close() @@ -103,6 +99,11 @@ async def chat(req: func.HttpRequest) -> func.HttpResponse: response_body = response[0].text.value + details = {"email": params.user_email, "Name: ": params.user_name, "prompt": response_body} + async with EventGridPublisher() as publisher: + await publisher.send_event(event = PromptToUserEvent(details).to_cloudevent()) + logging.info(f"Antwort-Prompt an {params.user_email} an EventGrid gesendet.") + try: return func.HttpResponse(response_body, status_code=http_status, headers={"Content-Type": "text/plain; charset=utf-8"}) except Exception as e: