nguyenbh commited on
Commit
7fc85f7
·
1 Parent(s): 692b3f2

Update app

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -1,20 +1,17 @@
1
  import os
2
  import gradio as gr
3
  from azure.ai.inference import ChatCompletionsClient
 
4
  from azure.core.credentials import AzureKeyCredential
5
 
6
 
7
  # Azure Inference setup
8
  url = os.getenv("Azure_Endpoint")
9
  api_key = AzureKeyCredential(os.getenv("Azure_API_KEY"))
10
-
11
 
12
  # Initialize the ChatCompletionsClient
13
- client = ChatCompletionsClient(
14
- endpoint=url,
15
- credential=api_key,
16
- stream=True
17
- )
18
 
19
  # Get and print model information (optional)
20
  try:
@@ -46,32 +43,35 @@ def get_azure_response(message, chat_history, temperature, max_tokens, top_p, pr
46
  Function to get a response from the Azure Phi-4 model
47
  """
48
  # Prepare conversation history in the format expected by Azure
49
- messages = [{"role": "system", "content": "You are a helpful AI assistant specialized in financial advice and planning."}]
50
 
51
  # Add conversation history
52
  for human, assistant in chat_history:
53
- messages.append({"role": "user", "content": human})
54
- if assistant: # Only add non-empty assistant messages
55
- messages.append({"role": "assistant", "content": assistant})
56
 
57
  # Add the current message
58
- messages.append({"role": "user", "content": message})
59
-
60
- # Prepare the payload
61
- payload = {
62
- "messages": messages,
63
  "max_tokens": max_tokens,
64
  "temperature": temperature,
65
  "top_p": top_p,
66
- "presence_penalty": presence_penalty,
67
- "frequency_penalty": frequency_penalty,
68
- "stream": True
69
  }
70
 
71
  # Get response
72
  try:
73
  print("Sending request to Azure...")
74
- response = client.complete(payload)
 
 
 
 
 
75
  return response
76
  except Exception as e:
77
  print(f"Error getting response: {str(e)}")
 
1
  import os
2
  import gradio as gr
3
  from azure.ai.inference import ChatCompletionsClient
4
+ from azure.ai.inference.models import SystemMessage, UserMessage, AssistantMessage
5
  from azure.core.credentials import AzureKeyCredential
6
 
7
 
8
  # Azure Inference setup
9
  url = os.getenv("Azure_Endpoint")
10
  api_key = AzureKeyCredential(os.getenv("Azure_API_KEY"))
11
+ model_name = os.getenv("Azure_Model_Name")
12
 
13
  # Initialize the ChatCompletionsClient
14
+ client = ChatCompletionsClient(endpoint=url, credential=api_key, stream=True)
 
 
 
 
15
 
16
  # Get and print model information (optional)
17
  try:
 
43
  Function to get a response from the Azure Phi-4 model
44
  """
45
  # Prepare conversation history in the format expected by Azure
46
+ messages=[SystemMessage("You are a helpful AI assistant.")]
47
 
48
  # Add conversation history
49
  for human, assistant in chat_history:
50
+ messages.append(UserMessage(human))
51
+ if assistant:
52
+ messages.append(AssistantMessage(assistant))
53
 
54
  # Add the current message
55
+ messages.append(UserMessage(message))
56
+
57
+ # Prepare the model_extras
58
+ model_extras = {
 
59
  "max_tokens": max_tokens,
60
  "temperature": temperature,
61
  "top_p": top_p,
62
+ "presence_penalty": 0,
63
+ "frequency_penalty": 0,
 
64
  }
65
 
66
  # Get response
67
  try:
68
  print("Sending request to Azure...")
69
+ response = client.complete(
70
+ model=model_name,
71
+ messages=messages,
72
+ stream=True,
73
+ model_extras=model_extras
74
+ )
75
  return response
76
  except Exception as e:
77
  print(f"Error getting response: {str(e)}")