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

XFEstudio/gpt4free

Add Llama2 Providers / Models

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

代码差异

4 个文件 +91 -9
Added g4f/Provider/DeepInfra.py +63 -0
@@ -0,0 +1,63 @@
1 from __future__ import annotations
2
3 import json
4 from aiohttp import ClientSession
5
6 from ..typing import AsyncResult, Messages
7 from .base_provider import AsyncGeneratorProvider
8
9
10 class DeepInfra(AsyncGeneratorProvider):
11 url = "https://deepinfra.com"
12 working = True
13
14 @classmethod
15 async def create_async_generator(
16 cls,
17 model: str,
18 messages: Messages,
19 proxy: str = None,
20 **kwargs
21 ) -> AsyncResult:
22 if not model:
23 model = "meta-llama/Llama-2-70b-chat-hf"
24 headers = {
25 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
26 "Accept": "text/event-stream",
27 "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
28 "Accept-Encoding": "gzip, deflate, br",
29 "Referer": f"{cls.url}/",
30 "Content-Type": "application/json",
31 "X-Deepinfra-Source": "web-page",
32 "Origin": cls.url,
33 "Connection": "keep-alive",
34 "Sec-Fetch-Dest": "empty",
35 "Sec-Fetch-Mode": "cors",
36 "Sec-Fetch-Site": "same-site",
37 "Pragma": "no-cache",
38 "Cache-Control": "no-cache",
39 }
40 async with ClientSession(headers=headers) as session:
41 data = {
42 "model": model,
43 "messages": messages,
44 "stream": True,
45 }
46 async with session.post(
47 "https://api.deepinfra.com/v1/openai/chat/completions",
48 json=data,
49 proxy=proxy
50 ) as response:
51 response.raise_for_status()
52 first = True
53 async for line in response.content:
54 if line.startswith(b"data: [DONE]"):
55 break
56 elif line.startswith(b"data: "):
57 chunk = json.loads(line[6:])["choices"][0]["delta"].get("content")
58 if chunk:
59 if first:
60 chunk = chunk.lstrip()
61 if chunk:
62 first = False
63 yield chunk
Modified g4f/Provider/Llama2.py +8 -9
@@ -6,15 +6,14 @@ from ..typing import AsyncResult, Messages
6 6 from .base_provider import AsyncGeneratorProvider
7 7
8 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"},
9 "meta-llama/Llama-2-7b-chat-hf": {"name": "Llama 2 7B", "version": "d24902e3fa9b698cc208b5e63136c4e26e828659a9f09827ca6ec5bb83014381", "shortened":"7B"},
10 "meta-llama/Llama-2-13b-chat-hf": {"name": "Llama 2 13B", "version": "9dff94b1bed5af738655d4a7cbcdcde2bd503aa85c94334fe1f42af7f3dd5ee3", "shortened":"13B"},
11 "meta-llama/Llama-2-70b-chat-hf": {"name": "Llama 2 70B", "version": "2796ee9483c3fd7aa2e171d38f4ca12251a30609463dcfd4cd76703f22e96cdf", "shortened":"70B"},
12 12 "Llava": {"name": "Llava 13B", "version": "6bc1c7bb0d2a34e413301fee8f7cc728d2d4e75bfab186aa995f63292bda92fc", "shortened":"Llava"}
13 13 }
14 14
15 15 class Llama2(AsyncGeneratorProvider):
16 16 url = "https://www.llama2.ai"
17 supports_gpt_35_turbo = True
18 17 working = True
19 18
20 19 @classmethod
@@ -26,8 +25,8 @@ class Llama2(AsyncGeneratorProvider):
26 25 **kwargs
27 26 ) -> AsyncResult:
28 27 if not model:
29 model = "70B"
30 if model not in models:
28 model = "meta-llama/Llama-2-70b-chat-hf"
29 elif model not in models:
31 30 raise ValueError(f"Model are not supported: {model}")
32 31 version = models[model]["version"]
33 32 headers = {
@@ -54,7 +53,7 @@ class Llama2(AsyncGeneratorProvider):
54 53 "systemPrompt": kwargs.get("system_message", "You are a helpful assistant."),
55 54 "temperature": kwargs.get("temperature", 0.75),
56 55 "topP": kwargs.get("top_p", 0.9),
57 "maxTokens": kwargs.get("max_tokens", 1024),
56 "maxTokens": kwargs.get("max_tokens", 8000),
58 57 "image": None
59 58 }
60 59 started = False
@@ -68,9 +67,9 @@ class Llama2(AsyncGeneratorProvider):
68 67
69 68 def format_prompt(messages: Messages):
70 69 messages = [
71 f"[INST]{message['content']}[/INST]"
70 f"[INST] {message['content']} [/INST]"
72 71 if message["role"] == "user"
73 72 else message["content"]
74 73 for message in messages
75 74 ]
76 return "\n".join(messages)
75 return "\n".join(messages) + "\n"
Modified g4f/Provider/__init__.py +3 -0
@@ -17,6 +17,7 @@ from .ChatgptFree import ChatgptFree
17 17 from .ChatgptLogin import ChatgptLogin
18 18 from .ChatgptX import ChatgptX
19 19 from .Cromicle import Cromicle
20 from .DeepInfra import DeepInfra
20 21 from .FakeGpt import FakeGpt
21 22 from .FreeGpt import FreeGpt
22 23 from .GPTalk import GPTalk
@@ -70,6 +71,7 @@ class ProviderUtils:
70 71 'ChatgptX': ChatgptX,
71 72 'CodeLinkAva': CodeLinkAva,
72 73 'Cromicle': Cromicle,
74 'DeepInfra': DeepInfra,
73 75 'DfeHub': DfeHub,
74 76 'EasyChat': EasyChat,
75 77 'Equing': Equing,
@@ -144,6 +146,7 @@ __all__ = [
144 146 'ChatgptLogin',
145 147 'ChatgptX',
146 148 'Cromicle',
149 'DeepInfra',
147 150 'CodeLinkAva',
148 151 'DfeHub',
149 152 'EasyChat',
Modified g4f/models.py +17 -0
@@ -6,12 +6,14 @@ from .Provider import (
6 6 GptForLove,
7 7 ChatgptAi,
8 8 GptChatly,
9 DeepInfra,
9 10 ChatgptX,
10 11 ChatBase,
11 12 GeekGpt,
12 13 FakeGpt,
13 14 FreeGpt,
14 15 NoowAi,
16 Llama2,
15 17 Vercel,
16 18 Aichat,
17 19 GPTalk,
@@ -74,6 +76,21 @@ gpt_4 = Model(
74 76 ])
75 77 )
76 78
79 llama2_7b = Model(
80 name = "meta-llama/Llama-2-7b-chat-hf",
81 base_provider = 'huggingface',
82 best_provider = RetryProvider([Llama2, DeepInfra]))
83
84 llama2_13b = Model(
85 name ="meta-llama/Llama-2-13b-chat-hf",
86 base_provider = 'huggingface',
87 best_provider = RetryProvider([Llama2, DeepInfra]))
88
89 llama2_70b = Model(
90 name = "meta-llama/Llama-2-70b-chat-hf",
91 base_provider = "huggingface",
92 best_provider = RetryProvider([Llama2, DeepInfra]))
93
77 94 # Bard
78 95 palm = Model(
79 96 name = 'palm',