Spaces:
Runtime error
Runtime error
Delete chat.py
Browse files
chat.py
DELETED
@@ -1,137 +0,0 @@
|
|
1 |
-
import logging
|
2 |
-
import httpx
|
3 |
-
import os
|
4 |
-
from dotenv import load_dotenv
|
5 |
-
|
6 |
-
# Load environment variables from .env file
|
7 |
-
load_dotenv()
|
8 |
-
|
9 |
-
# Get the API key and endpoint from environment variables
|
10 |
-
api_key = os.getenv('AZURE_OPENAI_API_KEY')
|
11 |
-
endpoint = os.getenv('AZURE_OPENAI_ENDPOINT')
|
12 |
-
|
13 |
-
def list_fine_tuning_jobs() -> dict:
|
14 |
-
"""List fine-tuning jobs.
|
15 |
-
|
16 |
-
Returns:
|
17 |
-
dict: The response from the API containing the list of fine-tuning jobs.
|
18 |
-
"""
|
19 |
-
try:
|
20 |
-
headers = {
|
21 |
-
"Content-Type": "application/json",
|
22 |
-
"Authorization": f"Bearer {api_key}"
|
23 |
-
}
|
24 |
-
response = httpx.get(f"{endpoint}/openai/fine-tunes", headers=headers)
|
25 |
-
response.raise_for_status()
|
26 |
-
return response.json()
|
27 |
-
except httpx.HTTPStatusError as e:
|
28 |
-
logging.error(f"Error listing fine-tuning jobs: {e.response.text}")
|
29 |
-
return None
|
30 |
-
except Exception as e:
|
31 |
-
logging.error(f"Unexpected error: {e}")
|
32 |
-
return None
|
33 |
-
|
34 |
-
def upload_file_for_fine_tuning(file_path: str) -> dict:
|
35 |
-
"""Upload a file for fine-tuning.
|
36 |
-
|
37 |
-
Args:
|
38 |
-
file_path (str): The path to the file to be uploaded.
|
39 |
-
|
40 |
-
Returns:
|
41 |
-
dict: The response from the API after uploading the file.
|
42 |
-
"""
|
43 |
-
try:
|
44 |
-
headers = {
|
45 |
-
"Authorization": f"Bearer {api_key}"
|
46 |
-
}
|
47 |
-
with open(file_path, 'rb') as file:
|
48 |
-
files = {'file': file}
|
49 |
-
response = httpx.post(f"{endpoint}/openai/files", headers=headers, files=files)
|
50 |
-
response.raise_for_status()
|
51 |
-
return response.json()
|
52 |
-
except httpx.HTTPStatusError as e:
|
53 |
-
logging.error(f"Error uploading file for fine-tuning: {e.response.text}")
|
54 |
-
return None
|
55 |
-
except Exception as e:
|
56 |
-
logging.error(f"Unexpected error: {e}")
|
57 |
-
return None
|
58 |
-
|
59 |
-
def create_fine_tuning_job(training_file_id: str, model: str = "davinci") -> dict:
|
60 |
-
"""Create a fine-tuning job.
|
61 |
-
|
62 |
-
Args:
|
63 |
-
training_file_id (str): The ID of the training file.
|
64 |
-
model (str): The model to be fine-tuned. Default is "davinci".
|
65 |
-
|
66 |
-
Returns:
|
67 |
-
dict: The response from the API after creating the fine-tuning job.
|
68 |
-
"""
|
69 |
-
try:
|
70 |
-
headers = {
|
71 |
-
"Content-Type": "application/json",
|
72 |
-
"Authorization": f"Bearer {api_key}"
|
73 |
-
}
|
74 |
-
payload = {
|
75 |
-
"training_file": training_file_id,
|
76 |
-
"model": model
|
77 |
-
}
|
78 |
-
response = httpx.post(f"{endpoint}/openai/fine-tunes", headers=headers, json=payload)
|
79 |
-
response.raise_for_status()
|
80 |
-
return response.json()
|
81 |
-
except httpx.HTTPStatusError as e:
|
82 |
-
logging.error(f"Error creating fine-tuning job: {e.response.text}")
|
83 |
-
return None
|
84 |
-
except Exception as e:
|
85 |
-
logging.error(f"Unexpected error: {e}")
|
86 |
-
return None
|
87 |
-
|
88 |
-
def make_post_request(url: str, data: dict, headers: dict) -> dict:
|
89 |
-
"""Make a POST request.
|
90 |
-
|
91 |
-
Args:
|
92 |
-
url (str): The URL to make the POST request to.
|
93 |
-
data (dict): The data to be sent in the POST request.
|
94 |
-
headers (dict): The headers to be sent in the POST request.
|
95 |
-
|
96 |
-
Returns:
|
97 |
-
dict: The response from the API after making the POST request.
|
98 |
-
"""
|
99 |
-
try:
|
100 |
-
response = httpx.post(url, json=data, headers=headers)
|
101 |
-
response.raise_for_status()
|
102 |
-
return response.json()
|
103 |
-
except httpx.HTTPStatusError as e:
|
104 |
-
logging.error(f"Error making POST request: {e.response.text}")
|
105 |
-
return None
|
106 |
-
except Exception as e:
|
107 |
-
logging.error(f"Unexpected error: {e}")
|
108 |
-
return None
|
109 |
-
|
110 |
-
def azure_chat_completion_request(messages: list, deployment_id: str) -> str:
|
111 |
-
"""Make a chat completion request to Azure OpenAI.
|
112 |
-
|
113 |
-
Args:
|
114 |
-
messages (list): The list of messages to be sent in the chat completion request.
|
115 |
-
deployment_id (str): The deployment ID for the chat completion request.
|
116 |
-
|
117 |
-
Returns:
|
118 |
-
str: The response content from the chat completion request.
|
119 |
-
"""
|
120 |
-
try:
|
121 |
-
headers = {
|
122 |
-
"Content-Type": "application/json",
|
123 |
-
"Authorization": f"Bearer {api_key}"
|
124 |
-
}
|
125 |
-
payload = {
|
126 |
-
"deployment_id": deployment_id,
|
127 |
-
"messages": messages
|
128 |
-
}
|
129 |
-
response = httpx.post(f"{endpoint}/openai/deployments/{deployment_id}/chat/completions", headers=headers, json=payload)
|
130 |
-
response.raise_for_status()
|
131 |
-
return response.json()["choices"][0]["message"]["content"].strip()
|
132 |
-
except httpx.HTTPStatusError as e:
|
133 |
-
logging.error(f"Error making chat completion request: {e.response.text}")
|
134 |
-
return None
|
135 |
-
except Exception as e:
|
136 |
-
logging.error(f"Unexpected error: {e}")
|
137 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|