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

XFEstudio/gpt4free

Add Llama2 and NoowAi Provider

c1adfbee
Heiner Lohaus <heiner@lohaus.eu>
提交于

代码差异

8 个文件 +158 -10
Modified README.md +2 -2
@@ -325,12 +325,12 @@ asyncio.run(run_all())
325 325
326 326 ##### Proxy Support:
327 327
328 All providers support specifying a proxy in the create function.
328 All providers support specifying a proxy in the create functions.
329 329
330 330 ```py
331 331 import g4f
332 332
333 response = await g4f.ChatCompletion.create(
333 response = g4f.ChatCompletion.create(
334 334 model=g4f.models.default,
335 335 messages=[{"role": "user", "content": "Hello"}],
336 336 proxy="http://host:port",
Modified g4f/Provider/ChatForAi.py +1 -1
@@ -44,7 +44,7 @@ class ChatForAi(AsyncGeneratorProvider):
44 44 **kwargs
45 45 },
46 46 "botSettings": {},
47 "prompt": prompt,
47 "prompt": prompt,
48 48 "messages": messages,
49 49 "timestamp": timestamp,
50 50 "sign": generate_signature(timestamp, prompt, conversation_id)
Added g4f/Provider/Llama2.py +76 -0
@@ -0,0 +1,76 @@
1 from __future__ import annotations
2
3 from aiohttp import ClientSession
4
5 from ..typing import AsyncResult, Messages
6 from .base_provider import AsyncGeneratorProvider
7
8 models = {
9 "7B": {"name": "Llama 2 7B", "version": "d24902e3fa9b698cc208b5e63136c4e26e828659a9f09827ca6ec5bb83014381", "shortened":"7B"},
10 "13B": {"name": "Llama 2 13B", "version": "9dff94b1bed5af738655d4a7cbcdcde2bd503aa85c94334fe1f42af7f3dd5ee3", "shortened":"13B"},
11 "70B": {"name": "Llama 2 70B", "version": "2796ee9483c3fd7aa2e171d38f4ca12251a30609463dcfd4cd76703f22e96cdf", "shortened":"70B"},
12 "Llava": {"name": "Llava 13B", "version": "6bc1c7bb0d2a34e413301fee8f7cc728d2d4e75bfab186aa995f63292bda92fc", "shortened":"Llava"}
13 }
14
15 class Llama2(AsyncGeneratorProvider):
16 url = "https://www.llama2.ai"
17 supports_gpt_35_turbo = True
18 working = True
19
20 @classmethod
21 async def create_async_generator(
22 cls,
23 model: str,
24 messages: Messages,
25 proxy: str = None,
26 **kwargs
27 ) -> AsyncResult:
28 if not model:
29 model = "70B"
30 if model not in models:
31 raise ValueError(f"Model are not supported: {model}")
32 version = models[model]["version"]
33 headers = {
34 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
35 "Accept": "*/*",
36 "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
37 "Accept-Encoding": "gzip, deflate, br",
38 "Referer": f"{cls.url}/",
39 "Content-Type": "text/plain;charset=UTF-8",
40 "Origin": cls.url,
41 "Connection": "keep-alive",
42 "Sec-Fetch-Dest": "empty",
43 "Sec-Fetch-Mode": "cors",
44 "Sec-Fetch-Site": "same-origin",
45 "Pragma": "no-cache",
46 "Cache-Control": "no-cache",
47 "TE": "trailers"
48 }
49 async with ClientSession(headers=headers) as session:
50 prompt = format_prompt(messages)
51 data = {
52 "prompt": prompt,
53 "version": version,
54 "systemPrompt": kwargs.get("system_message", "You are a helpful assistant."),
55 "temperature": kwargs.get("temperature", 0.75),
56 "topP": kwargs.get("top_p", 0.9),
57 "maxTokens": kwargs.get("max_tokens", 1024),
58 "image": None
59 }
60 started = False
61 async with session.post(f"{cls.url}/api", json=data, proxy=proxy) as response:
62 response.raise_for_status()
63 async for chunk in response.content.iter_any():
64 if not started:
65 chunk = chunk.lstrip()
66 started = True
67 yield chunk.decode()
68
69 def format_prompt(messages: Messages):
70 messages = [
71 f"[INST]{message['content']}[/INST]"
72 if message["role"] == "user"
73 else message["content"]
74 for message in messages
75 ]
76 return "\n".join(messages)
Added g4f/Provider/NoowAi.py +66 -0
@@ -0,0 +1,66 @@
1 from __future__ import annotations
2
3 import random, string, json
4 from aiohttp import ClientSession
5
6 from ..typing import AsyncResult, Messages
7 from .base_provider import AsyncGeneratorProvider
8
9
10 class NoowAi(AsyncGeneratorProvider):
11 url = "https://noowai.com"
12 supports_gpt_35_turbo = True
13 working = True
14
15 @classmethod
16 async def create_async_generator(
17 cls,
18 model: str,
19 messages: Messages,
20 proxy: str = None,
21 **kwargs
22 ) -> AsyncResult:
23 headers = {
24 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
25 "Accept": "*/*",
26 "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
27 "Accept-Encoding": "gzip, deflate, br",
28 "Referer": f"{cls.url}/",
29 "Content-Type": "application/json",
30 "Origin": cls.url,
31 "Alt-Used": "noowai.com",
32 "Connection": "keep-alive",
33 "Sec-Fetch-Dest": "empty",
34 "Sec-Fetch-Mode": "cors",
35 "Sec-Fetch-Site": "same-origin",
36 "Pragma": "no-cache",
37 "Cache-Control": "no-cache",
38 "TE": "trailers"
39 }
40 async with ClientSession(headers=headers) as session:
41 data = {
42 "botId": "default",
43 "customId": "d49bc3670c3d858458576d75c8ea0f5d",
44 "session": "N/A",
45 "chatId": random_string(),
46 "contextId": 25,
47 "messages": messages,
48 "newMessage": messages[-1]["content"],
49 "stream": True
50 }
51 async with session.post(f"{cls.url}/wp-json/mwai-ui/v1/chats/submit", json=data, proxy=proxy) as response:
52 response.raise_for_status()
53 async for line in response.content:
54 if line.startswith(b"data: "):
55 try:
56 line = json.loads(line[6:])
57 assert "type" in line
58 except:
59 raise RuntimeError(f"Broken line: {line.decode()}")
60 if line["type"] == "live":
61 yield line["data"]
62 elif line["type"] == "end":
63 break
64
65 def random_string(length: int = 10):
66 return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(length))
Modified g4f/Provider/__init__.py +6 -1
@@ -23,9 +23,10 @@ from .GptChatly import GptChatly
23 23 from .GptForLove import GptForLove
24 24 from .GptGo import GptGo
25 25 from .GptGod import GptGod
26 from .H2o import H2o
27 26 from .Liaobots import Liaobots
27 from .Llama2 import Llama2
28 28 from .Myshell import Myshell
29 from .NoowAi import NoowAi
29 30 from .Opchatgpts import Opchatgpts
30 31 from .Phind import Phind
31 32 from .Vercel import Vercel
@@ -82,9 +83,11 @@ class ProviderUtils:
82 83 'HuggingChat': HuggingChat,
83 84 'Komo': Komo,
84 85 'Liaobots': Liaobots,
86 'Llama2': Llama2,
85 87 'Lockchat': Lockchat,
86 88 'MikuChat': MikuChat,
87 89 'Myshell': Myshell,
90 'NoowAi': NoowAi,
88 91 'Opchatgpts': Opchatgpts,
89 92 'OpenAssistant': OpenAssistant,
90 93 'OpenaiChat': OpenaiChat,
@@ -148,8 +151,10 @@ __all__ = [
148 151 'H2o',
149 152 'HuggingChat',
150 153 'Liaobots',
154 'Llama2',
151 155 'Lockchat',
152 156 'Myshell',
157 'NoowAi',
153 158 'Opchatgpts',
154 159 'Raycast',
155 160 'OpenaiChat',
Renamed g4f/Provider/deprecated/H2o.py +2 -4
@@ -5,13 +5,12 @@ import uuid
5 5
6 6 from aiohttp import ClientSession
7 7
8 from ..typing import AsyncResult, Messages
9 from .base_provider import AsyncGeneratorProvider, format_prompt
8 from ...typing import AsyncResult, Messages
9 from ..base_provider import AsyncGeneratorProvider, format_prompt
10 10
11 11
12 12 class H2o(AsyncGeneratorProvider):
13 13 url = "https://gpt-gm.h2o.ai"
14 working = False
15 14 model = "h2oai/h2ogpt-gm-oasst1-en-2048-falcon-40b-v1"
16 15
17 16 @classmethod
@@ -86,7 +85,6 @@ class H2o(AsyncGeneratorProvider):
86 85 async with session.delete(
87 86 f"{cls.url}/conversation/{conversationId}",
88 87 proxy=proxy,
89 json=data
90 88 ) as response:
91 89 response.raise_for_status()
92 90
Modified g4f/Provider/deprecated/__init__.py +2 -1
@@ -11,4 +11,5 @@ from .Wuguokai import Wuguokai
11 11 from .V50 import V50
12 12 from .FastGpt import FastGpt
13 13 from .Aivvm import Aivvm
14 from .Vitalentum import Vitalentum
14 from .Vitalentum import Vitalentum
15 from .H2o import H2o
Modified g4f/models.py +3 -1
@@ -16,6 +16,7 @@ from .Provider import (
16 16 Yqcloud,
17 17 Myshell,
18 18 FreeGpt,
19 NoowAi,
19 20 Vercel,
20 21 Aichat,
21 22 GPTalk,
@@ -51,8 +52,9 @@ gpt_35_long = Model(
51 52 name = 'gpt-3.5-turbo',
52 53 base_provider = 'openai',
53 54 best_provider = RetryProvider([
54 AiAsk, Aichat, ChatgptDemo, FreeGpt, GptGo, Liaobots, You,
55 AiAsk, Aichat, ChatgptDemo, FreeGpt, Liaobots, You,
55 56 GPTalk, ChatgptLogin, GptChatly, GptForLove, Opchatgpts,
57 NoowAi,
56 58 ])
57 59 )
58 60