Skip to content

Commit

Permalink
Events fertiggestellt; Prompts verändert; Function Calls laufen
Browse files Browse the repository at this point in the history
  • Loading branch information
TechPrototyper committed Apr 10, 2024
1 parent 68b32cf commit 1fc59ec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
30 changes: 28 additions & 2 deletions API/azure_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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}")
Expand Down Expand Up @@ -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)."
Expand Down
17 changes: 9 additions & 8 deletions API/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,21 +82,15 @@ 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)
# logging.info(f"Chat-Endpoint came back: Response: {http_status}: {response}")

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()
Expand All @@ -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:
Expand Down

0 comments on commit 1fc59ec

Please sign in to comment.