FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
openai-python/upload_knowledge.py at main · SonicX/openai-python · GitHub
SonicX
/
openai-python
Public
forked from
openai/openai-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
openai-python
/
upload_knowledge.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
199 lines (161 loc) · 7.45 KB
Breadcrumbs
openai-python
/
upload_knowledge.py
Copy path
File metadata and controls
199 lines (161 loc) · 7.45 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
"""
Скрипт для загрузки файлов в качестве знаний на сервер
Использование: python upload_knowledge.py <путь_к_файлу> <api_key> [опциональные_параметры]
"""
import
os
import
sys
import
json
import
mimetypes
import
requests
from
pathlib
import
Path
from
typing
import
Optional
,
Dict
,
Any
def
validate_file_path
(
file_path
:
str
)
->
bool
:
"""Проверяет существование файла и его доступность для чтения"""
path
=
Path
(
file_path
)
if
not
path
.
exists
():
print
(
f"Ошибка: Файл
{
file_path
}
не существует"
)
return
False
if
not
path
.
is_file
():
print
(
f"Ошибка:
{
file_path
}
не является файлом"
)
return
False
if
not
os
.
access
(
path
,
os
.
R_OK
):
print
(
f"Ошибка: Нет прав на чтение файла
{
file_path
}
"
)
return
False
return
True
def
get_file_type
(
file_path
:
str
)
->
str
:
"""Определяет тип файла по расширению и MIME-типу"""
path
=
Path
(
file_path
)
extension
=
path
.
suffix
.
lower
()
# Поддерживаемые типы файлов
supported_types
=
{
'.txt'
:
'text/plain'
,
'.pdf'
:
'application/pdf'
,
'.doc'
:
'application/msword'
,
'.docx'
:
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
,
'.xls'
:
'application/vnd.ms-excel'
,
'.xlsx'
:
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
,
'.csv'
:
'text/csv'
,
'.json'
:
'application/json'
,
'.xml'
:
'application/xml'
,
'.html'
:
'text/html'
,
'.htm'
:
'text/html'
}
mime_type
,
_
=
mimetypes
.
guess_type
(
file_path
)
if
extension
in
supported_types
:
return
supported_types
[
extension
]
elif
mime_type
:
return
mime_type
else
:
return
'application/octet-stream'
# Бинарный тип по умолчанию
def
read_file_content
(
file_path
:
str
)
->
Optional
[
str
]:
"""Читает содержимое файла и возвращает его как строку"""
try
:
# Определяем тип файла
file_type
=
get_file_type
(
file_path
)
# Для текстовых файлов читаем напрямую
if
file_type
.
startswith
(
'text/'
)
or
file_type
in
[
'application/json'
,
'application/xml'
,
'text/html'
]:
with
open
(
file_path
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
return
f
.
read
()
# Для бинарных файлов (PDF, DOCX и т.д.) возвращаем содержимое как байты
# которые затем можно закодировать или обработать соответствующей библиотекой
else
:
with
open
(
file_path
,
'rb'
)
as
f
:
return
f
.
read
().
decode
(
'utf-8'
,
errors
=
'ignore'
)
except
UnicodeDecodeError
:
# Если возникла ошибка декодирования, пробуем другие кодировки
try
:
with
open
(
file_path
,
'rb'
)
as
f
:
raw_data
=
f
.
read
()
# Пробуем декодировать как CP1251 (часто используется для русских документов)
return
raw_data
.
decode
(
'cp1251'
)
except
UnicodeDecodeError
:
print
(
f"Ошибка: Не удалось декодировать файл
{
file_path
}
"
)
return
None
except
Exception
as
e
:
print
(
f"Ошибка при чтении файла
{
file_path
}
:
{
e
}
"
)
return
None
def
prepare_file_data
(
file_path
:
str
)
->
Dict
[
str
,
Any
]:
"""Подготавливает данные файла для отправки"""
file_path_obj
=
Path
(
file_path
)
return
{
'filename'
:
file_path_obj
.
name
,
'filepath'
:
str
(
file_path_obj
.
absolute
()),
'size'
:
file_path_obj
.
stat
().
st_size
,
'content'
:
read_file_content
(
file_path
),
'file_type'
:
get_file_type
(
file_path
)
}
def
upload_to_server
(
file_data
:
Dict
[
str
,
Any
],
api_key
:
str
,
server_url
:
str
)
->
bool
:
"""
Отправляет файл на сервер
ВАЖНО: Здесь нужно заменить на реальный API вашего сервера
"""
headers
=
{
'Authorization'
:
f'Bearer
{
api_key
}
'
,
'Content-Type'
:
'application/json'
}
# Подготовка данных для отправки
payload
=
{
'filename'
:
file_data
[
'filename'
],
'file_type'
:
file_data
[
'file_type'
],
'content'
:
file_data
[
'content'
][:
10000
],
# Ограничиваем длину для примера
'size'
:
file_data
[
'size'
]
}
try
:
print
(
"Отправка файла на сервер..."
)
response
=
requests
.
post
(
server_url
,
json
=
payload
,
headers
=
headers
,
timeout
=
30
)
if
response
.
status_code
in
[
200
,
201
]:
print
(
"✓ Файл успешно загружен на сервер"
)
print
(
f"Ответ сервера:
{
response
.
json
()
}
"
)
return
True
else
:
print
(
f"✗ Ошибка при загрузке файла:
{
response
.
status_code
}
"
)
print
(
f"Ответ сервера:
{
response
.
text
}
"
)
return
False
except
requests
.
exceptions
.
ConnectionError
:
print
(
"✗ Ошибка подключения к серверу"
)
return
False
except
requests
.
exceptions
.
Timeout
:
print
(
"✗ Время ожидания истекло"
)
return
False
except
Exception
as
e
:
print
(
f"✗ Произошла ошибка при отправке файла:
{
e
}
"
)
return
False
def
main
():
if
len
(
sys
.
argv
)
<
3
:
print
(
"Использование: python upload_knowledge.py <путь_к_файлу> <api_key> [server_url]"
)
print
(
"Пример: python upload_knowledge.py ./document.pdf abc123def456 https://my-server.com/api/upload"
)
sys
.
exit
(
1
)
file_path
=
sys
.
argv
[
1
]
api_key
=
sys
.
argv
[
2
]
server_url
=
sys
.
argv
[
3
]
if
len
(
sys
.
argv
)
>
3
else
"https://example.com/api/upload"
# Значение по умолчанию
print
(
f"Файл:
{
file_path
}
"
)
print
(
f"API ключ:
{
'*'
*
len
(
api_key
)
}
(
{
len
(
api_key
)
}
символов)"
)
print
(
f"URL сервера:
{
server_url
}
"
)
# Проверяем файл
if
not
validate_file_path
(
file_path
):
sys
.
exit
(
1
)
# Получаем информацию о файле
print
(
"
\n
Чтение файла..."
)
file_data
=
prepare_file_data
(
file_path
)
if
file_data
[
'content'
]
is
None
:
print
(
"Не удалось прочитать содержимое файла"
)
sys
.
exit
(
1
)
print
(
f"✓ Файл прочитан. Размер:
{
file_data
[
'size'
]
}
байт"
)
print
(
f"Тип файла:
{
file_data
[
'file_type'
]
}
"
)
# Отправляем файл на сервер
success
=
upload_to_server
(
file_data
,
api_key
,
server_url
)
if
success
:
print
(
"
\n
✓ Загрузка завершена успешно"
)
else
:
print
(
"
\n
✗ Загрузка не удалась"
)
sys
.
exit
(
1
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL