Update app.py
Browse files
app.py
CHANGED
@@ -162,27 +162,28 @@ class TextEditor:
|
|
162 |
|
163 |
# OpenAIApi class
|
164 |
class OpenAIApi:
|
165 |
-
def __init__(self, preprompt="", endpoint="https://
|
166 |
# Validate endpoint format
|
167 |
-
if "
|
168 |
-
logger.
|
169 |
-
raise ValueError("Invalid endpoint: Replace '<your-resource-name>' with your Azure OpenAI resource name (e.g., https://my-resource.openai.azure.com/)")
|
170 |
-
if not re.match(r"^https://[a-zA-Z0-9-]+\.openai\.azure\.com/?$", endpoint):
|
171 |
-
logger.warning(f"Endpoint format may be incorrect: {endpoint}. Expected format: https://<resource-name>.openai.azure.com/")
|
172 |
|
173 |
# Use a minimal httpx.Client to avoid proxies parameter
|
174 |
http_client = httpx.Client()
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
|
|
|
|
|
|
181 |
self.model = model
|
182 |
-
self.preprompt =
|
183 |
self.memory = ConversationMemory()
|
184 |
self.editor = TextEditor(self.memory)
|
185 |
-
logger.info(f"Initialized OpenAIApi with endpoint: {endpoint}, model: {model}, api_version: 2024-02-15-preview")
|
186 |
self.functions = [
|
187 |
{
|
188 |
"type": "function",
|
@@ -386,7 +387,7 @@ async def edit_suffix(chunk_id, suffix):
|
|
386 |
def create_ui():
|
387 |
with gr.Blocks(title="Azure OpenAI Chat & Text Editor") as demo:
|
388 |
gr.Markdown("# Azure OpenAI Chat with Text Editing")
|
389 |
-
gr.Markdown("**Note**:
|
390 |
|
391 |
with gr.Tab("Chat"):
|
392 |
chatbot = gr.Chatbot(label="Conversation", type="messages")
|
|
|
162 |
|
163 |
# OpenAIApi class
|
164 |
class OpenAIApi:
|
165 |
+
def __init__(self, preprompt="", endpoint="https://T-App-GPT4o.openai.azure.com/", model="gpt-4o", api_key=None):
|
166 |
# Validate endpoint format
|
167 |
+
if not re.match(r"^https://[a-zA-Z0-9-]+\.openai\.com/?$", endpoint):
|
168 |
+
logger.warning(f"Endpoint format may be incorrect: {endpoint}. Expected format: https://<resource>.openai.azure.com/")
|
|
|
|
|
|
|
169 |
|
170 |
# Use a minimal httpx.Client to avoid proxies parameter
|
171 |
http_client = httpx.Client()
|
172 |
+
try:
|
173 |
+
self.client = AzureOpenAI(
|
174 |
+
azure_endpoint=endpoint,
|
175 |
+
api_key=api_key or os.getenv("AZURE_OPENAI_API_KEY"),
|
176 |
+
api_version="2024-02-15-preview",
|
177 |
+
http_client=http_client
|
178 |
+
)
|
179 |
+
except Exception as e:
|
180 |
+
logger.error(f"Failed to initialize AzureOpenAI client: {str(e)}")
|
181 |
+
raise
|
182 |
self.model = model
|
183 |
+
self.preprompt = prompt
|
184 |
self.memory = ConversationMemory()
|
185 |
self.editor = TextEditor(self.memory)
|
186 |
+
logger.info(f"Initialized OpenAIApi with: endpoint: {endpoint}, model: {model}, api_version: 2024-02-15-preview}")
|
187 |
self.functions = [
|
188 |
{
|
189 |
"type": "function",
|
|
|
387 |
def create_ui():
|
388 |
with gr.Blocks(title="Azure OpenAI Chat & Text Editor") as demo:
|
389 |
gr.Markdown("# Azure OpenAI Chat with Text Editing")
|
390 |
+
gr.Markdown("**Note**: Using Azure OpenAI endpoint: https://T-App-GPT4o.openai.azure.com/")
|
391 |
|
392 |
with gr.Tab("Chat"):
|
393 |
chatbot = gr.Chatbot(label="Conversation", type="messages")
|