返回提交历史
Modified
g4f/Provider/GeminiPro.py
+9
-7
Modified
g4f/Provider/Liaobots.py
+1
-1
Modified
g4f/gui/client/js/chat.v1.js
+20
-17
XFEstudio/gpt4free
gui: remove cursor on errors Add auth header to GeminiPro provider
b4b74c99
代码差异
3 个文件
+30
-25
@@ -32,17 +32,20 @@ class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin):
32
32
model = "gemini-pro-vision" if not model and image else model
33
33
model = cls.get_model(model)
34
34
35
if not api_key and not api_base:
36
raise MissingAuthError('Missing "api_key" or "api_base"')
35
if not api_key:
36
raise MissingAuthError('Missing "api_key"')
37
37
if not api_base:
38
38
api_base = f"https://generativelanguage.googleapis.com/v1beta"
39
39
40
40
method = "streamGenerateContent" if stream else "generateContent"
41
41
url = f"{api_base.rstrip('/')}/models/{model}:{method}"
42
if api_key:
42
headers = None
43
if api_base:
44
headers = {f"Authorization": "Bearer {api_key}"}
45
else:
43
46
url += f"?key={api_key}"
44
47
45
async with ClientSession() as session:
48
async with ClientSession(headers=headers) as session:
46
49
contents = [
47
50
{
48
51
"role": "model" if message["role"] == "assistant" else message["role"],
@@ -79,12 +82,11 @@ class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin):
79
82
lines = [b"{\n"]
80
83
elif chunk == b",\r\n" or chunk == b"]":
81
84
try:
82
data = b"".join(lines)
83
data = json.loads(data)
85
data = json.loads(b"".join(lines))
84
86
yield data["candidates"][0]["content"]["parts"][0]["text"]
85
87
except:
86
88
data = data.decode() if isinstance(data, bytes) else data
87
raise RuntimeError(f"Read chunk failed. data: {data}")
89
raise RuntimeError(f"Read chunk failed: {data}")
88
90
lines = []
89
91
else:
90
92
lines.append(chunk)
@@ -78,7 +78,7 @@ class Liaobots(AsyncGeneratorProvider, ProviderModelMixin):
78
78
supports_gpt_35_turbo = True
79
79
supports_gpt_4 = True
80
80
default_model = "gpt-3.5-turbo"
81
models = [m for m in models]
81
models = list(models)
82
82
model_aliases = {
83
83
"claude-v2": "claude-2"
84
84
}
@@ -3,7 +3,6 @@ const markdown = window.markdownit();
3
3
const message_box = document.getElementById(`messages`);
4
4
const message_input = document.getElementById(`message-input`);
5
5
const box_conversations = document.querySelector(`.top`);
6
const spinner = box_conversations.querySelector(".spinner");
7
6
const stop_generating = document.querySelector(`.stop_generating`);
8
7
const regenerate = document.querySelector(`.regenerate`);
9
8
const send_button = document.querySelector(`#send-button`);
@@ -71,6 +70,7 @@ const handle_ask = async () => {
71
70
message_input.style.height = `82px`;
72
71
message_input.focus();
73
72
window.scrollTo(0, 0);
73
74
74
message = message_input.value
75
75
if (message.length > 0) {
76
76
message_input.value = '';
@@ -292,13 +292,16 @@ const ask_gpt = async () => {
292
292
if (!error) {
293
293
await add_message(window.conversation_id, "assistant", text, provider);
294
294
await load_conversation(window.conversation_id);
295
} else {
296
let cursorDiv = document.getElementById(`cursor`);
297
if (cursorDiv) cursorDiv.parentNode.removeChild(cursorDiv);
295
298
}
296
299
message_box.scrollTop = message_box.scrollHeight;
297
300
await remove_cancel_button();
298
301
await register_remove_message();
299
302
prompt_lock = false;
300
303
window.scrollTo(0, 0);
301
await load_conversations(20, 0);
304
await load_conversations();
302
305
regenerate.classList.remove(`regenerate-hidden`);
303
306
};
304
307
@@ -357,7 +360,7 @@ const delete_conversation = async (conversation_id) => {
357
360
await new_conversation();
358
361
}
359
362
360
await load_conversations(20, 0, true);
363
await load_conversations();
361
364
};
362
365
363
366
const set_conversation = async (conversation_id) => {
@@ -366,7 +369,7 @@ const set_conversation = async (conversation_id) => {
366
369
367
370
await clear_conversation();
368
371
await load_conversation(conversation_id);
369
await load_conversations(20, 0, true);
372
await load_conversations();
370
373
};
371
374
372
375
const new_conversation = async () => {
@@ -374,7 +377,7 @@ const new_conversation = async () => {
374
377
window.conversation_id = uuid();
375
378
376
379
await clear_conversation();
377
await load_conversations(20, 0, true);
380
await load_conversations();
378
381
379
382
await say_hello()
380
383
};
@@ -439,14 +442,14 @@ function count_words(text) {
439
442
}
440
443
441
444
function count_tokens(model, text) {
442
if (model.startsWith("gpt-3") || model.startsWith("gpt-4")) {
443
return GPTTokenizer_cl100k_base?.encode(text).length
445
if (model.startsWith("gpt-3") || model.startsWith("gpt-4") || model.startsWith("text-davinci")) {
446
return GPTTokenizer_cl100k_base?.encode(text).length;
444
447
}
445
448
if (model.startsWith("llama2") || model.startsWith("codellama")) {
446
return llamaTokenizer?.encode(text).length
449
return llamaTokenizer?.encode(text).length;
447
450
}
448
451
if (model.startsWith("mistral") || model.startsWith("mixtral")) {
449
return mistralTokenizer?.encode(text).length
452
return mistralTokenizer?.encode(text).length;
450
453
}
451
454
}
452
455
@@ -530,7 +533,7 @@ const add_message = async (conversation_id, role, content, provider) => {
530
533
return conversation.items.length - 1;
531
534
};
532
535
533
const load_conversations = async (limit, offset, loader) => {
536
const load_conversations = async () => {
534
537
let conversations = [];
535
538
for (let i = 0; i < localStorage.length; i++) {
536
539
if (localStorage.key(i).startsWith("conversation:")) {
@@ -554,7 +557,6 @@ const load_conversations = async (limit, offset, loader) => {
554
557
</div>
555
558
`;
556
559
}
557
558
560
};
559
561
560
562
document.getElementById(`cancelButton`).addEventListener(`click`, async () => {
@@ -697,10 +699,8 @@ window.onload = async () => {
697
699
}
698
700
}
699
701
700
if (conversations == 0) localStorage.clear();
701
702
702
await setTimeout(() => {
703
load_conversations(20, 0);
703
load_conversations();
704
704
}, 1);
705
705
706
706
if (/\/chat\/.+/.test(window.location.href)) {
@@ -780,15 +780,17 @@ observer.observe(message_input, { attributes: true });
780
780
versions = await response.json()
781
781
782
782
document.title = 'g4f - gui - ' + versions["version"];
783
text = "version ~ "
783
let text = "version ~ "
784
784
if (versions["version"] != versions["latest_version"]) {
785
release_url = 'https://github.com/xtekky/gpt4free/releases/tag/' + versions["latest_version"];
786
text += '<a href="' + release_url +'" target="_blank" title="New version: ' + versions["latest_version"] +'">' + versions["version"] + ' 🆕</a>';
785
let release_url = 'https://github.com/xtekky/gpt4free/releases/tag/' + versions["latest_version"];
786
let title = `New version: ${versions["latest_version"]}`;
787
text += `<a href="${release_url}" target="_blank" title="${title}">${versions["version"]} 🆕</a>`;
787
788
} else {
788
789
text += versions["version"];
789
790
}
790
791
document.getElementById("version_text").innerHTML = text
791
792
})()
793
792
794
for (const el of [imageInput, cameraInput]) {
793
795
el.addEventListener('click', async () => {
794
796
el.value = '';
@@ -798,6 +800,7 @@ for (const el of [imageInput, cameraInput]) {
798
800
}
799
801
});
800
802
}
803
801
804
fileInput.addEventListener('click', async (event) => {
802
805
fileInput.value = '';
803
806
delete fileInput.dataset.text;