XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/gpt4free

~

automatic models fetching in GUI.

d4ab83a4
abc <98614666+xtekky@users.noreply.github.com>
提交于

代码差异

6 个文件 +163 -151
Modified g4f/Provider/Vercel.py +72 -60
@@ -2,9 +2,10 @@ from __future__ import annotations
2 2
3 3 import json, base64, requests, execjs, random, uuid
4 4
5 from ..typing import Messages, TypedDict, CreateResult
5 from ..typing import Messages, TypedDict, CreateResult, Any
6 6 from .base_provider import BaseProvider
7 7 from abc import abstractmethod
8 from ..debug import logging
8 9
9 10
10 11 class Vercel(BaseProvider):
@@ -19,14 +20,16 @@ class Vercel(BaseProvider):
19 20 model: str,
20 21 messages: Messages,
21 22 stream: bool,
22 proxy: str = None,
23 **kwargs
24 ) -> CreateResult:
23 proxy: str = None, **kwargs) -> CreateResult:
24
25 print(model)
26
25 27 if not model:
26 28 model = "gpt-3.5-turbo"
29
27 30 elif model not in model_info:
28 raise ValueError(f"Model are not supported: {model}")
29
31 raise ValueError(f"Vercel does not support {model}")
32
30 33 headers = {
31 34 'authority' : 'sdk.vercel.ai',
32 35 'accept' : '*/*',
@@ -110,40 +113,49 @@ class ModelInfo(TypedDict):
110 113 default_params: dict[str, Any]
111 114
112 115 model_info: dict[str, ModelInfo] = {
113 'claude-instant-v1': {
114 'id': 'anthropic:claude-instant-v1',
115 'default_params': {
116 'temperature': 1,
117 'maximumLength': 1024,
118 'topP': 1,
119 'topK': 1,
120 'presencePenalty': 1,
121 'frequencyPenalty': 1,
122 'stopSequences': ['\n\nHuman:'],
123 },
124 },
125 'claude-v1': {
126 'id': 'anthropic:claude-v1',
127 'default_params': {
128 'temperature': 1,
129 'maximumLength': 1024,
130 'topP': 1,
131 'topK': 1,
132 'presencePenalty': 1,
133 'frequencyPenalty': 1,
134 'stopSequences': ['\n\nHuman:'],
135 },
136 },
137 'claude-v2': {
138 'id': 'anthropic:claude-v2',
116 # 'claude-instant-v1': {
117 # 'id': 'anthropic:claude-instant-v1',
118 # 'default_params': {
119 # 'temperature': 1,
120 # 'maximumLength': 1024,
121 # 'topP': 1,
122 # 'topK': 1,
123 # 'presencePenalty': 1,
124 # 'frequencyPenalty': 1,
125 # 'stopSequences': ['\n\nHuman:'],
126 # },
127 # },
128 # 'claude-v1': {
129 # 'id': 'anthropic:claude-v1',
130 # 'default_params': {
131 # 'temperature': 1,
132 # 'maximumLength': 1024,
133 # 'topP': 1,
134 # 'topK': 1,
135 # 'presencePenalty': 1,
136 # 'frequencyPenalty': 1,
137 # 'stopSequences': ['\n\nHuman:'],
138 # },
139 # },
140 # 'claude-v2': {
141 # 'id': 'anthropic:claude-v2',
142 # 'default_params': {
143 # 'temperature': 1,
144 # 'maximumLength': 1024,
145 # 'topP': 1,
146 # 'topK': 1,
147 # 'presencePenalty': 1,
148 # 'frequencyPenalty': 1,
149 # 'stopSequences': ['\n\nHuman:'],
150 # },
151 # },
152 'replicate/llama70b-v2-chat': {
153 'id': 'replicate:replicate/llama-2-70b-chat',
139 154 'default_params': {
140 'temperature': 1,
141 'maximumLength': 1024,
155 'temperature': 0.75,
156 'maximumLength': 3000,
142 157 'topP': 1,
143 'topK': 1,
144 'presencePenalty': 1,
145 'frequencyPenalty': 1,
146 'stopSequences': ['\n\nHuman:'],
158 'repetitionPenalty': 1,
147 159 },
148 160 },
149 161 'a16z-infra/llama7b-v2-chat': {
@@ -254,28 +266,28 @@ model_info: dict[str, ModelInfo] = {
254 266 'stopSequences': [],
255 267 },
256 268 },
257 'gpt-4': {
258 'id': 'openai:gpt-4',
259 'default_params': {
260 'temperature': 0.7,
261 'maximumLength': 8192,
262 'topP': 1,
263 'presencePenalty': 0,
264 'frequencyPenalty': 0,
265 'stopSequences': [],
266 },
267 },
268 'gpt-4-0613': {
269 'id': 'openai:gpt-4-0613',
270 'default_params': {
271 'temperature': 0.7,
272 'maximumLength': 8192,
273 'topP': 1,
274 'presencePenalty': 0,
275 'frequencyPenalty': 0,
276 'stopSequences': [],
277 },
278 },
269 # 'gpt-4': {
270 # 'id': 'openai:gpt-4',
271 # 'default_params': {
272 # 'temperature': 0.7,
273 # 'maximumLength': 8192,
274 # 'topP': 1,
275 # 'presencePenalty': 0,
276 # 'frequencyPenalty': 0,
277 # 'stopSequences': [],
278 # },
279 # },
280 # 'gpt-4-0613': {
281 # 'id': 'openai:gpt-4-0613',
282 # 'default_params': {
283 # 'temperature': 0.7,
284 # 'maximumLength': 8192,
285 # 'topP': 1,
286 # 'presencePenalty': 0,
287 # 'frequencyPenalty': 0,
288 # 'stopSequences': [],
289 # },
290 # },
279 291 'code-davinci-002': {
280 292 'id': 'openai:code-davinci-002',
281 293 'default_params': {
Modified g4f/__init__.py +17 -24
@@ -1,14 +1,13 @@
1 1 from __future__ import annotations
2 from requests import get
3 from g4f.models import Model, ModelUtils
4 from .Provider import BaseProvider, RetryProvider
5 from .typing import Messages, CreateResult, Union, List
6 from .debug import logging
2 from requests import get
3 from .models import Model, ModelUtils, _all_models
4 from .Provider import BaseProvider, RetryProvider
5 from .typing import Messages, CreateResult, Union, List
6 from .debug import logging
7 7
8 version = '0.1.6.6'
8 version = '0.1.6.6'
9 9 version_check = True
10 10
11
12 11 def check_pypi_version() -> None:
13 12 try:
14 13 response = get("https://pypi.org/pypi/g4f/json").json()
@@ -20,7 +19,6 @@ def check_pypi_version() -> None:
20 19 except Exception as e:
21 20 print(f'Failed to check g4f pypi version: {e}')
22 21
23
24 22 def get_model_and_provider(model : Union[Model, str],
25 23 provider : Union[type[BaseProvider], None],
26 24 stream : bool,
@@ -56,7 +54,7 @@ def get_model_and_provider(model : Union[Model, str],
56 54
57 55 class ChatCompletion:
58 56 @staticmethod
59 def create(model: Union[Model, str],
57 def create(model : Union[Model, str],
60 58 messages : Messages,
61 59 provider : Union[type[BaseProvider], None] = None,
62 60 stream : bool = False,
@@ -76,12 +74,11 @@ class ChatCompletion:
76 74 return result if stream else ''.join(result)
77 75
78 76 @staticmethod
79 async def create_async(
80 model : Union[Model, str],
81 messages: Messages,
82 provider: Union[type[BaseProvider], None] = None,
83 stream : bool = False,
84 ignored : List[str] = None, **kwargs) -> str:
77 async def create_async(model : Union[Model, str],
78 messages : Messages,
79 provider : Union[type[BaseProvider], None] = None,
80 stream : bool = False,
81 ignored : List[str] = None, **kwargs) -> str:
85 82
86 83 if stream:
87 84 raise ValueError(f'"create_async" does not support "stream" argument')
@@ -90,17 +87,13 @@ class ChatCompletion:
90 87
91 88 return await provider.create_async(model.name, messages, **kwargs)
92 89
93
94 90 class Completion:
95 91 @staticmethod
96 def create(
97 model: str,
98 prompt: str,
99 provider: Union[type[BaseProvider], None] = None,
100 stream: bool = False,
101 ignored : List[str] = None,
102 **kwargs
103 ) -> Union[CreateResult, str]:
92 def create(model : Union[Model, str],
93 prompt : str,
94 provider : Union[type[BaseProvider], None] = None,
95 stream : bool = False,
96 ignored : List[str] = None, **kwargs) -> Union[CreateResult, str]:
104 97
105 98 allowed_models = [
106 99 'code-davinci-002',
Modified g4f/gui/client/html/index.html +1 -8
@@ -117,14 +117,7 @@
117 117 </div>
118 118 <div class="field">
119 119 <select name="model" id="model">
120 <option value="gpt-3.5-turbo" selected>gpt-3.5</option>
121 <option value="gpt-3.5-turbo-0613">gpt-3.5 fast</option>
122 <option value="gpt-3.5-turbo-16k">gpt-3.5 16k</option>
123 <option value="gpt-3.5-turbo-16k-0613">gpt-3.5 16k fast</option>
124 <option value="gpt-4">gpt-4</option>
125 <option value="gpt-4-0613">gpt-4 fast</option>
126 <option value="gpt-4-32k">gpt-4 32k</option>
127 <option value="gpt-4-32k-0613">gpt-4 32k fast</option>
120 <option value="gpt-3.5-turbo" selected>gpt-3.5-turbo</option>
128 121 </select>
129 122 </div>
130 123 <div class="field">
Modified g4f/gui/client/js/chat.v2.js +53 -54
@@ -1,13 +1,12 @@
1 const query = (obj) => Object.keys(obj).map((k) => encodeURIComponent(k) + "=" + encodeURIComponent(obj[k])).join("&");
2 const colorThemes = document.querySelectorAll('[name="theme"]');
3 const markdown = window.markdownit();
4 const message_box = document.getElementById(`messages`);
5 const message_input = document.getElementById(`message-input`);
1 const colorThemes = document.querySelectorAll('[name="theme"]');
2 const markdown = window.markdownit();
3 const message_box = document.getElementById(`messages`);
4 const message_input = document.getElementById(`message-input`);
6 5 const box_conversations = document.querySelector(`.top`);
7 const spinner = box_conversations.querySelector(".spinner");
8 const stop_generating = document.querySelector(`.stop_generating`);
9 const send_button = document.querySelector(`#send-button`);
10 let prompt_lock = false;
6 const spinner = box_conversations.querySelector(".spinner");
7 const stop_generating = document.querySelector(`.stop_generating`);
8 const send_button = document.querySelector(`#send-button`);
9 let prompt_lock = false;
11 10
12 11 hljs.addPlugin(new CopyButtonPlugin());
13 12
@@ -81,8 +80,6 @@ const ask_gpt = async (message) => {
81 80 </div>
82 81 `;
83 82
84 /* .replace(/(?:\r\n|\r|\n)/g, '<br>') */
85
86 83 message_box.scrollTop = message_box.scrollHeight;
87 84 window.scrollTo(0, 0);
88 85 await new Promise((r) => setTimeout(r, 500));
@@ -108,10 +105,8 @@ const ask_gpt = async (message) => {
108 105 method: `POST`,
109 106 signal: window.controller.signal,
110 107 headers: {
111 "content-type": `application/json`,
108 'content-type': `application/json`,
112 109 accept: `text/event-stream`,
113 // v: `1.0.0`,
114 // ts: Date.now().toString(),
115 110 },
116 111 body: JSON.stringify({
117 112 conversation_id: window.conversation_id,
@@ -123,12 +118,12 @@ const ask_gpt = async (message) => {
123 118 id: window.token,
124 119 content: {
125 120 conversation: await get_conversation(window.conversation_id),
126 internet_access: document.getElementById("switch").checked,
127 content_type: "text",
121 internet_access: document.getElementById(`switch`).checked,
122 content_type: `text`,
128 123 parts: [
129 124 {
130 125 content: message,
131 role: "user",
126 role: `user`,
132 127 },
133 128 ],
134 129 },
@@ -146,8 +141,7 @@ const ask_gpt = async (message) => {
146 141
147 142 text += chunk;
148 143
149 document.getElementById(`gpt_${window.token}`).innerHTML =
150 markdown.render(text);
144 document.getElementById(`gpt_${window.token}`).innerHTML = markdown.render(text);
151 145 document.querySelectorAll(`code`).forEach((el) => {
152 146 hljs.highlightElement(el);
153 147 });
@@ -169,6 +163,7 @@ const ask_gpt = async (message) => {
169 163
170 164 await load_conversations(20, 0);
171 165 window.scrollTo(0, 0);
166
172 167 } catch (e) {
173 168 add_message(window.conversation_id, "user", message);
174 169
@@ -227,19 +222,19 @@ const show_option = async (conversation_id) => {
227 222 const yes = document.getElementById(`yes-${conversation_id}`);
228 223 const not = document.getElementById(`not-${conversation_id}`);
229 224
230 conv.style.display = "none";
231 yes.style.display = "block";
232 not.style.display = "block";
225 conv.style.display = `none`;
226 yes.style.display = `block`;
227 not.style.display = `block`;
233 228 };
234 229
235 230 const hide_option = async (conversation_id) => {
236 231 const conv = document.getElementById(`conv-${conversation_id}`);
237 const yes = document.getElementById(`yes-${conversation_id}`);
238 const not = document.getElementById(`not-${conversation_id}`);
232 const yes = document.getElementById(`yes-${conversation_id}`);
233 const not = document.getElementById(`not-${conversation_id}`);
239 234
240 conv.style.display = "block";
241 yes.style.display = "none";
242 not.style.display = "none";
235 conv.style.display = `block`;
236 yes.style.display = `none`;
237 not.style.display = `none`;
243 238 };
244 239
245 240 const delete_conversation = async (conversation_id) => {
@@ -272,7 +267,7 @@ const new_conversation = async () => {
272 267 await clear_conversation();
273 268 await load_conversations(20, 0, true);
274 269
275 await make_announcement()
270 await say_hello()
276 271 };
277 272
278 273 const load_conversation = async (conversation_id) => {
@@ -287,15 +282,15 @@ const load_conversation = async (conversation_id) => {
287 282 <div class="user">
288 283 ${item.role == "assistant" ? gpt_image : user_image}
289 284 ${item.role == "assistant"
290 ? `<i class="fa-regular fa-phone-arrow-down-left"></i>`
291 : `<i class="fa-regular fa-phone-arrow-up-right"></i>`
292 }
285 ? `<i class="fa-regular fa-phone-arrow-down-left"></i>`
286 : `<i class="fa-regular fa-phone-arrow-up-right"></i>`
287 }
293 288 </div>
294 289 <div class="content">
295 290 ${item.role == "assistant"
296 ? markdown.render(item.content)
297 : item.content
298 }
291 ? markdown.render(item.content)
292 : item.content
293 }
299 294 </div>
300 295 </div>
301 296 `;
@@ -351,13 +346,10 @@ const add_message = async (conversation_id, role, content) => {
351 346 localStorage.setItem(
352 347 `conversation:${conversation_id}`,
353 348 JSON.stringify(before_adding)
354 ); // update conversation
349 );
355 350 };
356 351
357 352 const load_conversations = async (limit, offset, loader) => {
358 //console.log(loader);
359 //if (loader === undefined) box_conversations.appendChild(spinner);
360
361 353 let conversations = [];
362 354 for (let i = 0; i < localStorage.length; i++) {
363 355 if (localStorage.key(i).startsWith("conversation:")) {
@@ -366,7 +358,6 @@ const load_conversations = async (limit, offset, loader) => {
366 358 }
367 359 }
368 360
369 //if (loader === undefined) spinner.parentNode.removeChild(spinner)
370 361 await clear_conversations();
371 362
372 363 for (conversation of conversations) {
@@ -393,17 +384,6 @@ document.getElementById(`cancelButton`).addEventListener(`click`, async () => {
393 384 console.log(`aborted ${window.conversation_id}`);
394 385 });
395 386
396 function h2a(str1) {
397 var hex = str1.toString();
398 var str = "";
399
400 for (var n = 0; n < hex.length; n += 2) {
401 str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
402 }
403
404 return str;
405 }
406
407 387 const uuid = () => {
408 388 return `xxxxxxxx-xxxx-4xxx-yxxx-${Date.now().toString(16)}`.replace(
409 389 /[xy]/g,
@@ -476,7 +456,7 @@ const load_settings_localstorage = async () => {
476 456 });
477 457 };
478 458
479 const make_announcement = async () => {
459 const say_hello = async () => {
480 460 tokens = [`Hello`, `!`, ` How`,` can`, ` I`,` assist`,` you`,` today`,`?`]
481 461
482 462 message_box.innerHTML += `
@@ -569,8 +549,9 @@ window.onload = async () => {
569 549 await load_conversation(window.conversation_id);
570 550 }
571 551 }
572
573 await make_announcement()
552
553 await load_models();
554 await say_hello()
574 555
575 556 message_input.addEventListener(`keydown`, async (evt) => {
576 557 if (prompt_lock) return;
@@ -612,4 +593,22 @@ const observer = new MutationObserver((mutationsList) => {
612 593 }
613 594 });
614 595
615 observer.observe(message_input, { attributes: true });
596 observer.observe(message_input, { attributes: true });
597
598
599 const load_models = async () => {
600 response = await fetch('/backend-api/v2/models')
601 models = await response.json()
602
603 var MODELS_SELECT = document.getElementById('model');
604
605 for (model of models) {
606
607 // Create new option elements
608 var model_info = document.createElement('option');
609 model_info.value = model
610 model_info.text = model
611
612 MODELS_SELECT.appendChild(model_info);
613 }
614 }
Modified g4f/gui/server/backend.py +5 -2
@@ -24,9 +24,12 @@ class Backend_Api:
24 24 'methods': ['POST']
25 25 },
26 26 }
27
27
28 28 def models(self):
29 return {}
29 models = g4f._all_models
30 models.remove('oasst-sft-4-pythia-12b-epoch-3.5')
31
32 return models
30 33
31 34 def _gen_title(self):
32 35 return {
Modified g4f/models.py +15 -3
@@ -37,6 +37,10 @@ class Model:
37 37 name: str
38 38 base_provider: str
39 39 best_provider: Union[type[BaseProvider], RetryProvider] = None
40
41 @staticmethod
42 def __all__() -> list[str]:
43 return _all_models
40 44
41 45 default = Model(
42 46 name = "",
@@ -231,6 +235,11 @@ llama7b_v2_chat = Model(
231 235 base_provider = 'replicate',
232 236 best_provider = Vercel)
233 237
238 llama70b_v2_chat = Model(
239 name = 'replicate/llama70b-v2-chat',
240 base_provider = 'replicate',
241 best_provider = Vercel)
242
234 243
235 244 class ModelUtils:
236 245 convert: dict[str, Model] = {
@@ -260,9 +269,9 @@ class ModelUtils:
260 269 'llama-13b' : llama_13b,
261 270
262 271 # Vercel
263 'claude-instant-v1' : claude_instant_v1,
264 'claude-v1' : claude_v1,
265 'claude-v2' : claude_v2,
272 #'claude-instant-v1' : claude_instant_v1,
273 #'claude-v1' : claude_v1,
274 #'claude-v2' : claude_v2,
266 275 'command-nightly' : command_nightly,
267 276 'gpt-neox-20b' : gpt_neox_20b,
268 277 'santacoder' : santacoder,
@@ -274,6 +283,7 @@ class ModelUtils:
274 283 'text-curie-001' : text_curie_001,
275 284 'text-davinci-002' : text_davinci_002,
276 285 'text-davinci-003' : text_davinci_003,
286 'llama70b-v2-chat' : llama70b_v2_chat,
277 287 'llama13b-v2-chat' : llama13b_v2_chat,
278 288 'llama7b-v2-chat' : llama7b_v2_chat,
279 289
@@ -281,3 +291,5 @@ class ModelUtils:
281 291 'oasst-sft-4-pythia-12b-epoch-3.5' : oasst_sft_4_pythia_12b_epoch_35,
282 292 'command-light-nightly' : command_light_nightly,
283 293 }
294
295 _all_models = list(ModelUtils.convert.keys())