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

XFEstudio/gpt4free

Merge pull request #2812 from mkantwala/main

New provider added (g4f/Provider/TypeGPT.py)

f5bdae4d
mkantwala <148573993+mkantwala@users.noreply.github.com>
提交于

代码差异

2 个文件 +86 -0
Added g4f/Provider/TypeGPT.py +85 -0
@@ -0,0 +1,85 @@
1 from __future__ import annotations
2 import json
3 from aiohttp import ClientSession
4
5 from ..typing import AsyncResult, Messages
6 from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
7 from ..requests.raise_for_status import raise_for_status
8 from ..providers.response import FinishReason
9 from .helper import format_prompt
10
11 class TypeGPT(AsyncGeneratorProvider, ProviderModelMixin):
12 label = "TypeGpt"
13 url = "https://chat.typegpt.net"
14 api_endpoint = "https://chat.typegpt.net/api/openai/v1/chat/completions"
15
16 working = True
17 supports_stream = True
18 supports_system_message = True
19 supports_message_history = True
20
21 default_model = 'o3-mini'
22 models = [
23 default_model,"gemini-1.5-flash","deepseek-r1","gemini-pro",
24 ]
25
26 @classmethod
27 async def create_async_generator(
28 cls,
29 model: str,
30 messages: Messages,
31 proxy: str = None,
32 **kwargs
33 ) -> AsyncResult:
34 model = cls.get_model(model)
35
36 headers = {
37 "accept": "application/json, text/event-stream",
38 "accept-language": "en-US,en;q=0.9",
39 "content-type": "application/json",
40 "origin": cls.url,
41 "referer": f"{cls.url}/",
42 "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36"
43 }
44
45 # Prepare the prompt from messages; adjust as needed.
46 prompt = format_prompt(messages)
47 data = {
48 "messages": [{"role": "user", "content": prompt}],
49 "stream": True,
50 "model": model,
51 "temperature": 0.5,
52 "presence_penalty": 0,
53 "frequency_penalty": 0,
54 "top_p": 1
55 }
56
57 async with ClientSession(headers=headers) as session:
58 async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
59 await raise_for_status(response)
60
61 async for chunk in response.content:
62 if not chunk:
63 continue
64
65 text = chunk.decode(errors="ignore")
66
67 # The stream returns lines prefixed with "data: "
68 if not text.startswith("data: "):
69 continue
70
71 # Check for finish signal in the stream
72 if "[DONE]" in text:
73 yield FinishReason("stop")
74 return
75
76 try:
77 json_data = json.loads(text[6:])
78 content = json_data["choices"][0].get("delta", {}).get("content", "")
79 if content:
80 yield content
81 except json.JSONDecodeError:
82 continue
83 except Exception:
84 yield FinishReason("error")
85 return
Modified g4f/Provider/__init__.py +1 -0
@@ -41,6 +41,7 @@ from .Pizzagpt import Pizzagpt
41 41 from .PollinationsAI import PollinationsAI
42 42 from .PollinationsImage import PollinationsImage
43 43 from .TeachAnything import TeachAnything
44 from .TypeGPT import TypeGPT
44 45 from .You import You
45 46 from .Websim import Websim
46 47 from .Yqcloud import Yqcloud