Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
import openai
|
| 5 |
import google.generativeai as palm
|
| 6 |
|
| 7 |
-
|
| 8 |
llm_api_options = ["OpenAI API","Azure OpenAI API","Google PaLM API", "Llama 2"]
|
| 9 |
TEST_MESSAGE = "Write an introductory paragraph to explain Generative AI to the reader of this content."
|
| 10 |
openai_models = ["gpt-4", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0613", "gpt-3.5-turbo",
|
|
@@ -19,69 +17,60 @@ azure_deployment_name = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
|
|
| 19 |
google_palm_key = os.getenv("GOOGLE_PALM_AI_API_KEY")
|
| 20 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 21 |
azure_openai_api_key = os.getenv("AZURE_OPENAI_KEY")
|
|
|
|
| 22 |
|
| 23 |
def openai_text_completion(prompt: str, model: str):
|
| 24 |
try:
|
| 25 |
system_prompt: str = "Explain in detail to help student understand the concept.",
|
| 26 |
-
|
| 27 |
assistant_prompt: str = None,
|
| 28 |
-
|
| 29 |
-
|
| 30 |
messages = [
|
| 31 |
{"role": "user", "content": f"{prompt}"},
|
| 32 |
{"role": "system", "content": f"{system_prompt}"},
|
| 33 |
{"role": "assistant", "content": f"{assistant_prompt}"}
|
| 34 |
]
|
| 35 |
-
|
| 36 |
openai.api_key = openai_api_key
|
| 37 |
openai.api_version = '2020-11-07'
|
| 38 |
-
|
| 39 |
completion = openai.ChatCompletion.create(
|
| 40 |
model = model,
|
| 41 |
messages = messages,
|
| 42 |
-
temperature =
|
| 43 |
)
|
| 44 |
response = completion["choices"][0]["message"].content
|
| 45 |
return "", response
|
| 46 |
-
except
|
| 47 |
print(f"Exception Name: {type(exception).__name__}")
|
| 48 |
print(exception)
|
| 49 |
-
return f"
|
| 50 |
|
| 51 |
def azure_openai_text_completion(prompt: str, model: str):
|
| 52 |
try:
|
| 53 |
system_prompt: str = "Explain in detail to help student understand the concept.",
|
| 54 |
-
|
| 55 |
assistant_prompt: str = None,
|
| 56 |
-
|
| 57 |
messages = [
|
| 58 |
{"role": "user", "content": f"{prompt}"},
|
| 59 |
{"role": "system", "content": f"{system_prompt}"},
|
| 60 |
{"role": "assistant", "content": f"{assistant_prompt}"}
|
| 61 |
]
|
| 62 |
-
|
| 63 |
openai.api_key = azure_openai_api_key
|
| 64 |
openai.api_type = "azure"
|
| 65 |
openai.api_version = "2023-05-15"
|
| 66 |
openai.api_base = f"https://{azure_endpoint}.openai.azure.com"
|
| 67 |
-
|
| 68 |
completion = openai.ChatCompletion.create(
|
| 69 |
model = model,
|
| 70 |
engine = azure_deployment_name,
|
| 71 |
messages = messages,
|
| 72 |
-
temperature =
|
| 73 |
)
|
| 74 |
response = completion["choices"][0]["message"].content
|
| 75 |
return "", response
|
| 76 |
-
except
|
| 77 |
print(f"Exception Name: {type(exception).__name__}")
|
| 78 |
print(exception)
|
| 79 |
-
return f"
|
| 80 |
|
| 81 |
|
| 82 |
def palm_text_completion(prompt: str, model: str):
|
| 83 |
-
try:
|
| 84 |
-
temperature = 0.7
|
| 85 |
candidate_count = 1
|
| 86 |
top_k = 40
|
| 87 |
top_p = 0.95
|
|
@@ -103,10 +92,10 @@ def palm_text_completion(prompt: str, model: str):
|
|
| 103 |
prompt=prompt
|
| 104 |
)
|
| 105 |
return "", response.result
|
| 106 |
-
except
|
| 107 |
print(f"Exception Name: {type(exception).__name__}")
|
| 108 |
print(exception)
|
| 109 |
-
return f"
|
| 110 |
|
| 111 |
def test_handler(optionSelection, prompt: str = TEST_MESSAGE, openai_model_name: str ="gpt-4", google_model_name: str ="models/text-bison-001"):
|
| 112 |
match optionSelection:
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import openai
|
| 4 |
import google.generativeai as palm
|
| 5 |
|
|
|
|
| 6 |
llm_api_options = ["OpenAI API","Azure OpenAI API","Google PaLM API", "Llama 2"]
|
| 7 |
TEST_MESSAGE = "Write an introductory paragraph to explain Generative AI to the reader of this content."
|
| 8 |
openai_models = ["gpt-4", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0613", "gpt-3.5-turbo",
|
|
|
|
| 17 |
google_palm_key = os.getenv("GOOGLE_PALM_AI_API_KEY")
|
| 18 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 19 |
azure_openai_api_key = os.getenv("AZURE_OPENAI_KEY")
|
| 20 |
+
temperature = 0.7
|
| 21 |
|
| 22 |
def openai_text_completion(prompt: str, model: str):
|
| 23 |
try:
|
| 24 |
system_prompt: str = "Explain in detail to help student understand the concept.",
|
|
|
|
| 25 |
assistant_prompt: str = None,
|
|
|
|
|
|
|
| 26 |
messages = [
|
| 27 |
{"role": "user", "content": f"{prompt}"},
|
| 28 |
{"role": "system", "content": f"{system_prompt}"},
|
| 29 |
{"role": "assistant", "content": f"{assistant_prompt}"}
|
| 30 |
]
|
|
|
|
| 31 |
openai.api_key = openai_api_key
|
| 32 |
openai.api_version = '2020-11-07'
|
|
|
|
| 33 |
completion = openai.ChatCompletion.create(
|
| 34 |
model = model,
|
| 35 |
messages = messages,
|
| 36 |
+
temperature = temperature
|
| 37 |
)
|
| 38 |
response = completion["choices"][0]["message"].content
|
| 39 |
return "", response
|
| 40 |
+
except Exception as exception:
|
| 41 |
print(f"Exception Name: {type(exception).__name__}")
|
| 42 |
print(exception)
|
| 43 |
+
return f" openai_text_completion Error - {exception}", ""
|
| 44 |
|
| 45 |
def azure_openai_text_completion(prompt: str, model: str):
|
| 46 |
try:
|
| 47 |
system_prompt: str = "Explain in detail to help student understand the concept.",
|
|
|
|
| 48 |
assistant_prompt: str = None,
|
|
|
|
| 49 |
messages = [
|
| 50 |
{"role": "user", "content": f"{prompt}"},
|
| 51 |
{"role": "system", "content": f"{system_prompt}"},
|
| 52 |
{"role": "assistant", "content": f"{assistant_prompt}"}
|
| 53 |
]
|
|
|
|
| 54 |
openai.api_key = azure_openai_api_key
|
| 55 |
openai.api_type = "azure"
|
| 56 |
openai.api_version = "2023-05-15"
|
| 57 |
openai.api_base = f"https://{azure_endpoint}.openai.azure.com"
|
|
|
|
| 58 |
completion = openai.ChatCompletion.create(
|
| 59 |
model = model,
|
| 60 |
engine = azure_deployment_name,
|
| 61 |
messages = messages,
|
| 62 |
+
temperature = temperature
|
| 63 |
)
|
| 64 |
response = completion["choices"][0]["message"].content
|
| 65 |
return "", response
|
| 66 |
+
except Exception as exception:
|
| 67 |
print(f"Exception Name: {type(exception).__name__}")
|
| 68 |
print(exception)
|
| 69 |
+
return f" azure_openai_text_completion Error - {exception}", ""
|
| 70 |
|
| 71 |
|
| 72 |
def palm_text_completion(prompt: str, model: str):
|
| 73 |
+
try:
|
|
|
|
| 74 |
candidate_count = 1
|
| 75 |
top_k = 40
|
| 76 |
top_p = 0.95
|
|
|
|
| 92 |
prompt=prompt
|
| 93 |
)
|
| 94 |
return "", response.result
|
| 95 |
+
except Exception as exception:
|
| 96 |
print(f"Exception Name: {type(exception).__name__}")
|
| 97 |
print(exception)
|
| 98 |
+
return f" palm_text_completion Error - {exception}", ""
|
| 99 |
|
| 100 |
def test_handler(optionSelection, prompt: str = TEST_MESSAGE, openai_model_name: str ="gpt-4", google_model_name: str ="models/text-bison-001"):
|
| 101 |
match optionSelection:
|