Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added exceptions #99

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 50 additions & 23 deletions backend/utils/llm_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import os
import openai
import requests
from rest_framework import status
from rest_framework.response import Response


def process_history(history):
Expand All @@ -67,19 +69,32 @@ def get_gpt4_output(system_prompt, user_prompt, history):
messages.extend(history)
messages.append({"role": "user", "content": user_prompt})

response = openai.ChatCompletion.create(
engine=engine,
messages=messages,
temperature=0.7,
max_tokens=700,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None,
try:
response = openai.ChatCompletion.create(
engine=engine,
messages=messages,
temperature=0.7,
max_tokens=700,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None,
)
return response["choices"][0]["message"]["content"].strip()
except openai.InvalidRequestError as e:
message = "Prompt violates LLM policy. Please enter a new prompt."
st = status.HTTP_400_BAD_REQUEST
except KeyError as e:
message = "Invalid response from the LLM"
st = status.HTTP_500_INTERNAL_SERVER_ERROR
except Exception as e:
message = "An error occurred while interacting with LLM."
st = status.HTTP_500_INTERNAL_SERVER_ERROR
return Response(
{"message": message},
status=st,
)

return response["choices"][0]["message"]["content"].strip()


def get_gpt3_output(system_prompt, user_prompt, history):
openai.api_type = os.getenv("LLM_INTERACTIONS_OPENAI_API_TYPE")
Expand All @@ -92,20 +107,32 @@ def get_gpt3_output(system_prompt, user_prompt, history):
messages = [{"role": "system", "content": system_prompt}]
messages.extend(history)
messages.append({"role": "user", "content": user_prompt})

response = openai.ChatCompletion.create(
engine=engine,
messages=messages,
temperature=0.7,
max_tokens=700,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None,
try:
response = openai.ChatCompletion.create(
engine=engine,
messages=messages,
temperature=0.7,
max_tokens=700,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None,
)
return response["choices"][0]["message"]["content"].strip()
except openai.InvalidRequestError as e:
message = "Prompt violates LLM policy. Please enter a new prompt."
st = status.HTTP_400_BAD_REQUEST
except KeyError as e:
message = "Invalid response from the LLM"
st = status.HTTP_500_INTERNAL_SERVER_ERROR
except Exception as e:
message = "An error occurred while interacting with LLM."
st = status.HTTP_500_INTERNAL_SERVER_ERROR
return Response(
{"message": message},
status=st,
)

return response["choices"][0]["message"]["content"].strip()


def get_llama2_output(system_prompt, conv_history, user_prompt):
api_base = os.getenv("LLM_INTERACTION_LLAMA2_API_BASE")
Expand Down
Loading