mirror of https://github.com/xtekky/gpt4free
Merge pull request #276 from sudouser777/feature/packaging-changes
added gpt4free packagepull/280/head
commit
e4aac28186
@ -1,57 +0,0 @@
|
||||
import requests
|
||||
|
||||
class Completion:
|
||||
@staticmethod
|
||||
def create(prompt:str, cookieInput:str) -> str:
|
||||
# Initialize a session with custom headers
|
||||
session = Completion._initialize_session(cookieInput)
|
||||
|
||||
# Set the data that will be submitted
|
||||
payload = Completion._create_payload(prompt, ("ASSUME I HAVE FULL ACCESS TO COCALC. "))
|
||||
|
||||
# Submit the request and return the results
|
||||
return Completion._submit_request(session, payload)
|
||||
|
||||
@classmethod
|
||||
def _initialize_session(cls, conversationCookie) -> requests.Session:
|
||||
"""Initialize a session with custom headers for the request."""
|
||||
|
||||
session = requests.Session()
|
||||
headers = {
|
||||
'Accept': '*/*',
|
||||
'Accept-Language': 'en-US,en;q=0.5',
|
||||
'Origin': 'https://cocalc.com',
|
||||
'Referer': 'https://cocalc.com/api/v2/openai/chatgpt',
|
||||
'Cookie': conversationCookie,
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
|
||||
}
|
||||
session.headers.update(headers)
|
||||
|
||||
return session
|
||||
|
||||
@classmethod
|
||||
def _create_payload(
|
||||
cls,
|
||||
prompt: str,
|
||||
system_prompt: str
|
||||
) -> dict:
|
||||
|
||||
return {
|
||||
"input": prompt,
|
||||
"system": system_prompt,
|
||||
"tag": "next:index"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _submit_request(
|
||||
cls,
|
||||
session: requests.Session,
|
||||
payload: dict
|
||||
) -> str:
|
||||
|
||||
response = session.post(
|
||||
"https://cocalc.com/api/v2/openai/chatgpt", json=payload).json()
|
||||
return {
|
||||
"response":response["output"],
|
||||
"success":response["success"]
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
### Example: `forefront` (use like openai pypi package) <a name="example-forefront"></a>
|
||||
|
||||
```python
|
||||
import forefront
|
||||
|
||||
# create an account
|
||||
token = forefront.Account.create(logging=False)
|
||||
print(token)
|
||||
|
||||
# get a response
|
||||
for response in forefront.StreamingCompletion.create(token = token,
|
||||
prompt = 'hello world', model='gpt-4'):
|
||||
|
||||
print(response.completion.choices[0].text, end = '')
|
||||
print("")
|
||||
```
|
@ -1,154 +0,0 @@
|
||||
from json import loads
|
||||
from re import match
|
||||
from time import time, sleep
|
||||
from uuid import uuid4
|
||||
|
||||
from requests import post
|
||||
from tls_client import Session
|
||||
|
||||
from forefront.mail import Mail
|
||||
from forefront.typing import ForeFrontResponse
|
||||
|
||||
|
||||
class Account:
|
||||
@staticmethod
|
||||
def create(proxy=None, logging=False):
|
||||
|
||||
proxies = {
|
||||
'http': 'http://' + proxy,
|
||||
'https': 'http://' + proxy} if proxy else False
|
||||
|
||||
start = time()
|
||||
|
||||
mail = Mail(proxies)
|
||||
mail_token = None
|
||||
mail_adress = mail.get_mail()
|
||||
|
||||
# print(mail_adress)
|
||||
|
||||
client = Session(client_identifier='chrome110')
|
||||
client.proxies = proxies
|
||||
client.headers = {
|
||||
"origin": "https://accounts.forefront.ai",
|
||||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36",
|
||||
}
|
||||
|
||||
response = client.post('https://clerk.forefront.ai/v1/client/sign_ups?_clerk_js_version=4.32.6',
|
||||
data={
|
||||
"email_address": mail_adress
|
||||
}
|
||||
)
|
||||
try:
|
||||
trace_token = response.json()['response']['id']
|
||||
if logging: print(trace_token)
|
||||
except KeyError:
|
||||
return 'Failed to create account!'
|
||||
|
||||
response = client.post(
|
||||
f"https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/prepare_verification?_clerk_js_version=4.32.6",
|
||||
data={
|
||||
"strategy": "email_code",
|
||||
}
|
||||
)
|
||||
|
||||
if logging: print(response.text)
|
||||
|
||||
if not 'sign_up_attempt' in response.text:
|
||||
return 'Failed to create account!'
|
||||
|
||||
while True:
|
||||
sleep(1)
|
||||
for _ in mail.fetch_inbox():
|
||||
if logging: print(mail.get_message_content(_["id"]))
|
||||
mail_token = match(r"(\d){5,6}", mail.get_message_content(_["id"])).group(0)
|
||||
|
||||
if mail_token:
|
||||
break
|
||||
|
||||
if logging: print(mail_token)
|
||||
|
||||
response = client.post(
|
||||
f'https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/attempt_verification?_clerk_js_version=4.38.4',
|
||||
data={
|
||||
'code': mail_token,
|
||||
'strategy': 'email_code'
|
||||
})
|
||||
|
||||
if logging: print(response.json())
|
||||
|
||||
token = response.json()['client']['sessions'][0]['last_active_token']['jwt']
|
||||
|
||||
with open('accounts.txt', 'a') as f:
|
||||
f.write(f'{mail_adress}:{token}\n')
|
||||
|
||||
if logging: print(time() - start)
|
||||
|
||||
return token
|
||||
|
||||
|
||||
class StreamingCompletion:
|
||||
@staticmethod
|
||||
def create(
|
||||
token=None,
|
||||
chatId=None,
|
||||
prompt='',
|
||||
actionType='new',
|
||||
defaultPersona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default
|
||||
model='gpt-4') -> ForeFrontResponse:
|
||||
|
||||
if not token: raise Exception('Token is required!')
|
||||
if not chatId: chatId = str(uuid4())
|
||||
|
||||
headers = {
|
||||
'authority': 'chat-server.tenant-forefront-default.knative.chi.coreweave.com',
|
||||
'accept': '*/*',
|
||||
'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
|
||||
'authorization': 'Bearer ' + token,
|
||||
'cache-control': 'no-cache',
|
||||
'content-type': 'application/json',
|
||||
'origin': 'https://chat.forefront.ai',
|
||||
'pragma': 'no-cache',
|
||||
'referer': 'https://chat.forefront.ai/',
|
||||
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
|
||||
'sec-ch-ua-mobile': '?0',
|
||||
'sec-ch-ua-platform': '"macOS"',
|
||||
'sec-fetch-dest': 'empty',
|
||||
'sec-fetch-mode': 'cors',
|
||||
'sec-fetch-site': 'cross-site',
|
||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
|
||||
}
|
||||
|
||||
json_data = {
|
||||
'text': prompt,
|
||||
'action': actionType,
|
||||
'parentId': chatId,
|
||||
'workspaceId': chatId,
|
||||
'messagePersona': defaultPersona,
|
||||
'model': model
|
||||
}
|
||||
|
||||
for chunk in post('https://chat-server.tenant-forefront-default.knative.chi.coreweave.com/chat',
|
||||
headers=headers, json=json_data, stream=True).iter_lines():
|
||||
|
||||
if b'finish_reason":null' in chunk:
|
||||
data = loads(chunk.decode('utf-8').split('data: ')[1])
|
||||
token = data['choices'][0]['delta'].get('content')
|
||||
|
||||
if token != None:
|
||||
yield ForeFrontResponse({
|
||||
'id': chatId,
|
||||
'object': 'text_completion',
|
||||
'created': int(time()),
|
||||
'model': model,
|
||||
'choices': [{
|
||||
'text': token,
|
||||
'index': 0,
|
||||
'logprobs': None,
|
||||
'finish_reason': 'stop'
|
||||
}],
|
||||
'usage': {
|
||||
'prompt_tokens': len(prompt),
|
||||
'completion_tokens': len(token),
|
||||
'total_tokens': len(prompt) + len(token)
|
||||
}
|
||||
})
|
@ -1,36 +0,0 @@
|
||||
class ForeFrontResponse:
|
||||
class Completion:
|
||||
class Choices:
|
||||
def __init__(self, choice: dict) -> None:
|
||||
self.text = choice['text']
|
||||
self.content = self.text.encode()
|
||||
self.index = choice['index']
|
||||
self.logprobs = choice['logprobs']
|
||||
self.finish_reason = choice['finish_reason']
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f'''<__main__.APIResponse.Completion.Choices(\n text = {self.text.encode()},\n index = {self.index},\n logprobs = {self.logprobs},\n finish_reason = {self.finish_reason})object at 0x1337>'''
|
||||
|
||||
def __init__(self, choices: dict) -> None:
|
||||
self.choices = [self.Choices(choice) for choice in choices]
|
||||
|
||||
class Usage:
|
||||
def __init__(self, usage_dict: dict) -> None:
|
||||
self.prompt_tokens = usage_dict['prompt_tokens']
|
||||
self.completion_tokens = usage_dict['completion_tokens']
|
||||
self.total_tokens = usage_dict['total_tokens']
|
||||
|
||||
def __repr__(self):
|
||||
return f'''<__main__.APIResponse.Usage(\n prompt_tokens = {self.prompt_tokens},\n completion_tokens = {self.completion_tokens},\n total_tokens = {self.total_tokens})object at 0x1337>'''
|
||||
|
||||
def __init__(self, response_dict: dict) -> None:
|
||||
self.response_dict = response_dict
|
||||
self.id = response_dict['id']
|
||||
self.object = response_dict['object']
|
||||
self.created = response_dict['created']
|
||||
self.model = response_dict['model']
|
||||
self.completion = self.Completion(response_dict['choices'])
|
||||
self.usage = self.Usage(response_dict['usage'])
|
||||
|
||||
def json(self) -> dict:
|
||||
return self.response_dict
|
@ -0,0 +1,116 @@
|
||||
# gpt4free package
|
||||
|
||||
### What is it?
|
||||
|
||||
gpt4free is a python package that provides the access to gpt models like ChatGPT and gpt-4 models.
|
||||
|
||||
### Main Features
|
||||
|
||||
- It's free to use
|
||||
- Easy access
|
||||
|
||||
### Installation:
|
||||
|
||||
```bash
|
||||
pip install gpt4free
|
||||
```
|
||||
|
||||
#### Usage:
|
||||
|
||||
```python
|
||||
import gpt4free
|
||||
import gpt4free
|
||||
from gpt4free import Provider, quora, forefront
|
||||
|
||||
# usage You
|
||||
response = gpt4free.Completion.create(Provider.You, prompt='Write a poem on Lionel Messi')
|
||||
print(response)
|
||||
|
||||
# usage Poe
|
||||
token = quora.Account.create(logging=False)
|
||||
response = gpt4free.Completion.create(Provider.Poe, prompt='Write a poem on Lionel Messi', token=token, model='ChatGPT')
|
||||
print(response)
|
||||
|
||||
# usage forefront
|
||||
token = forefront.Account.create(logging=False)
|
||||
response = gpt4free.Completion.create(
|
||||
Provider.ForeFront, prompt='Write a poem on Lionel Messi', model='gpt-4', token=token
|
||||
)
|
||||
print(response)
|
||||
print(f'END')
|
||||
|
||||
# usage theb
|
||||
response = gpt4free.Completion.create(Provider.Theb, prompt='Write a poem on Lionel Messi')
|
||||
print(response)
|
||||
|
||||
# usage cocalc
|
||||
response = gpt4free.Completion.create(Provider.CoCalc, prompt='Write a poem on Lionel Messi', cookie_input='')
|
||||
print(response)
|
||||
|
||||
```
|
||||
|
||||
### Invocation Arguments
|
||||
|
||||
`gpt4free.Completion.create()` method has two required arguments
|
||||
|
||||
1. Provider: This is an enum representing different provider
|
||||
2. prompt: This is the user input
|
||||
|
||||
#### Keyword Arguments
|
||||
|
||||
Some of the keyword arguments are optional, while others are required.
|
||||
|
||||
- You:
|
||||
- `safe_search`: boolean - default value is `False`
|
||||
- `include_links`: boolean - default value is `False`
|
||||
- `detailed`: boolean - default value is `False`
|
||||
- Quora:
|
||||
- `token`: str - this needs to be provided by the user
|
||||
- `model`: str - default value is `gpt-4`.
|
||||
|
||||
(Available models: `['Sage', 'GPT-4', 'Claude+', 'Claude-instant', 'ChatGPT', 'Dragonfly', 'NeevaAI']`)
|
||||
- ForeFront:
|
||||
- `token`: str - this need to be provided by the user
|
||||
|
||||
- Theb:
|
||||
(no keyword arguments required)
|
||||
- CoCalc:
|
||||
- `cookie_input`: str - this needs to be provided by user
|
||||
|
||||
#### Token generation of quora
|
||||
```python
|
||||
from gpt4free import quora
|
||||
|
||||
token = quora.Account.create(logging=False)
|
||||
```
|
||||
|
||||
### Token generation of ForeFront
|
||||
```python
|
||||
from gpt4free import forefront
|
||||
|
||||
token = forefront.Account.create(logging=False)
|
||||
```
|
||||
|
||||
## Copyright:
|
||||
|
||||
This program is licensed under the [GNU GPL v3](https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
### Copyright Notice: <a name="copyright"></a>
|
||||
|
||||
```
|
||||
xtekky/gpt4free: multiple reverse engineered language-model api's to decentralise the ai industry.
|
||||
Copyright (C) 2023 xtekky
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
```
|
@ -0,0 +1,64 @@
|
||||
from enum import Enum
|
||||
|
||||
from gpt4free import cocalc
|
||||
from gpt4free import forefront
|
||||
from gpt4free import quora
|
||||
from gpt4free import theb
|
||||
from gpt4free import you
|
||||
|
||||
|
||||
class Provider(Enum):
|
||||
"""An enum representing different providers."""
|
||||
|
||||
You = 'you'
|
||||
Poe = 'poe'
|
||||
ForeFront = 'fore_front'
|
||||
Theb = 'theb'
|
||||
CoCalc = 'cocalc'
|
||||
|
||||
|
||||
class Completion:
|
||||
"""This class will be used for invoking the given provider"""
|
||||
|
||||
@staticmethod
|
||||
def create(provider: Provider, prompt: str, **kwargs) -> str:
|
||||
"""
|
||||
Invokes the given provider with given prompt and addition arguments and returns the string response
|
||||
|
||||
:param provider: an enum representing the provider to use while invoking
|
||||
:param prompt: input provided by the user
|
||||
:param kwargs: Additional keyword arguments to pass to the provider while invoking
|
||||
:return: A string representing the response from the provider
|
||||
"""
|
||||
if provider == Provider.Poe:
|
||||
return Completion.__poe_service(prompt, **kwargs)
|
||||
elif provider == Provider.You:
|
||||
return Completion.__you_service(prompt, **kwargs)
|
||||
elif provider == Provider.ForeFront:
|
||||
return Completion.__fore_front_service(prompt, **kwargs)
|
||||
elif provider == Provider.Theb:
|
||||
return Completion.__theb_service(prompt, **kwargs)
|
||||
elif provider == Provider.CoCalc:
|
||||
return Completion.__cocalc_service(prompt, **kwargs)
|
||||
else:
|
||||
raise Exception('Provider not exist, Please try again')
|
||||
|
||||
@staticmethod
|
||||
def __you_service(prompt: str, **kwargs) -> str:
|
||||
return you.Completion.create(prompt, **kwargs).text
|
||||
|
||||
@staticmethod
|
||||
def __poe_service(prompt: str, **kwargs) -> str:
|
||||
return quora.Completion.create(prompt=prompt, **kwargs).text
|
||||
|
||||
@staticmethod
|
||||
def __fore_front_service(prompt: str, **kwargs) -> str:
|
||||
return forefront.Completion.create(prompt=prompt, **kwargs).text
|
||||
|
||||
@staticmethod
|
||||
def __theb_service(prompt: str, **kwargs):
|
||||
return ''.join(theb.Completion.create(prompt=prompt))
|
||||
|
||||
@staticmethod
|
||||
def __cocalc_service(prompt: str, **kwargs):
|
||||
return cocalc.Completion.create(prompt, cookie_input=kwargs.get('cookie_input', '')).text
|
@ -0,0 +1,47 @@
|
||||
import requests
|
||||
from fake_useragent import UserAgent
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class CoCalcResponse(BaseModel):
|
||||
text: str
|
||||
status: bool
|
||||
|
||||
|
||||
class Completion:
|
||||
@staticmethod
|
||||
def create(prompt: str, cookie_input: str) -> CoCalcResponse:
|
||||
# Initialize a session with custom headers
|
||||
session = Completion._initialize_session(cookie_input)
|
||||
|
||||
# Set the data that will be submitted
|
||||
payload = Completion._create_payload(prompt, 'ASSUME I HAVE FULL ACCESS TO COCALC. ')
|
||||
|
||||
# Submit the request and return the results
|
||||
return Completion._submit_request(session, payload)
|
||||
|
||||
@classmethod
|
||||
def _initialize_session(cls, conversation_cookie) -> requests.Session:
|
||||
"""Initialize a session with custom headers for the request."""
|
||||
|
||||
session = requests.Session()
|
||||
headers = {
|
||||
'Accept': '*/*',
|
||||
'Accept-Language': 'en-US,en;q=0.5',
|
||||
'Origin': 'https://cocalc.com',
|
||||
'Referer': 'https://cocalc.com/api/v2/openai/chatgpt',
|
||||
'Cookie': conversation_cookie,
|
||||
'User-Agent': UserAgent().random,
|
||||
}
|
||||
session.headers.update(headers)
|
||||
|
||||
return session
|
||||
|
||||
@staticmethod
|
||||
def _create_payload(prompt: str, system_prompt: str) -> dict:
|
||||
return {'input': prompt, 'system': system_prompt, 'tag': 'next:index'}
|
||||
|
||||
@staticmethod
|
||||
def _submit_request(session: requests.Session, payload: dict) -> CoCalcResponse:
|
||||
response = session.post('https://cocalc.com/api/v2/openai/chatgpt', json=payload).json()
|
||||
return CoCalcResponse(text=response['output'], status=response['success'])
|
@ -1,11 +1,10 @@
|
||||
### Example: `cocalc` <a name="example-cocalc"></a>
|
||||
|
||||
|
||||
```python
|
||||
# import library
|
||||
import cocalc
|
||||
from gpt4free import cocalc
|
||||
|
||||
cocalc.Completion.create(prompt="How are you!", cookieInput="cookieinput") ## Tutorial
|
||||
cocalc.Completion.create(prompt="How are you!", cookie_input="cookieinput") ## Tutorial
|
||||
```
|
||||
|
||||
### How to grab cookie input
|
@ -0,0 +1,16 @@
|
||||
### Example: `forefront` (use like openai pypi package) <a name="example-forefront"></a>
|
||||
|
||||
```python
|
||||
|
||||
from gpt4free import forefront
|
||||
|
||||
# create an account
|
||||
token = forefront.Account.create(logging=False)
|
||||
print(token)
|
||||
|
||||
# get a response
|
||||
for response in forefront.StreamingCompletion.create(token=token,
|
||||
prompt='hello world', model='gpt-4'):
|
||||
print(response.completion.choices[0].text, end='')
|
||||
print("")
|
||||
```
|
@ -0,0 +1,192 @@
|
||||
from json import loads
|
||||
from re import match
|
||||
from time import time, sleep
|
||||
from typing import Generator, Optional
|
||||
from uuid import uuid4
|
||||
|
||||
from fake_useragent import UserAgent
|
||||
from requests import post
|
||||
from tls_client import Session
|
||||
|
||||
from .mail import Mail
|
||||
from .typing import ForeFrontResponse
|
||||
|
||||
|
||||
class Account:
|
||||
@staticmethod
|
||||
def create(proxy: Optional[str] = None, logging: bool = False):
|
||||
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False
|
||||
|
||||
start = time()
|
||||
|
||||
mail_client = Mail(proxies)
|
||||
mail_token = None
|
||||
mail_address = mail_client.get_mail()
|
||||
|
||||
# print(mail_address)
|
||||
|
||||
client = Session(client_identifier='chrome110')
|
||||
client.proxies = proxies
|
||||
client.headers = {
|
||||
'origin': 'https://accounts.forefront.ai',
|
||||
'user-agent': UserAgent().random,
|
||||
}
|
||||
|
||||
response = client.post(
|
||||
'https://clerk.forefront.ai/v1/client/sign_ups?_clerk_js_version=4.32.6',
|
||||
data={'email_address': mail_address},
|
||||
)
|
||||
|
||||
try:
|
||||
trace_token = response.json()['response']['id']
|
||||
if logging:
|
||||
print(trace_token)
|
||||
except KeyError:
|
||||
return 'Failed to create account!'
|
||||
|
||||
response = client.post(
|
||||
f'https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/prepare_verification?_clerk_js_version=4.32.6',
|
||||
data={
|
||||
'strategy': 'email_code',
|
||||
},
|
||||
)
|
||||
|
||||
if logging:
|
||||
print(response.text)
|
||||
|
||||
if 'sign_up_attempt' not in response.text:
|
||||
return 'Failed to create account!'
|
||||
|
||||
while True:
|
||||
sleep(1)
|
||||
for _ in mail_client.fetch_inbox():
|
||||
if logging:
|
||||
print(mail_client.get_message_content(_['id']))
|
||||
mail_token = match(r'(\d){5,6}', mail_client.get_message_content(_['id'])).group(0)
|
||||
|
||||
if mail_token:
|
||||
break
|
||||
|
||||
if logging:
|
||||
print(mail_token)
|
||||
|
||||
response = client.post(
|
||||
f'https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/attempt_verification?_clerk_js_version=4.38.4',
|
||||
data={'code': mail_token, 'strategy': 'email_code'},
|
||||
)
|
||||
|
||||
if logging:
|
||||
print(response.json())
|
||||
|
||||
token = response.json()['client']['sessions'][0]['last_active_token']['jwt']
|
||||
|
||||
with open('accounts.txt', 'a') as f:
|
||||
f.write(f'{mail_address}:{token}\n')
|
||||
|
||||
if logging:
|
||||
print(time() - start)
|
||||
|
||||
return token
|
||||
|
||||
|
||||
class StreamingCompletion:
|
||||
@staticmethod
|
||||
def create(
|
||||
token=None,
|
||||
chat_id=None,
|
||||
prompt='',
|
||||
action_type='new',
|
||||
default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default
|
||||
model='gpt-4',
|
||||
) -> Generator[ForeFrontResponse, None, None]:
|
||||
if not token:
|
||||
raise Exception('Token is required!')
|
||||
if not chat_id:
|
||||
chat_id = str(uuid4())
|
||||
|
||||
headers = {
|
||||
'authority': 'chat-server.tenant-forefront-default.knative.chi.coreweave.com',
|
||||
'accept': '*/*',
|
||||
'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
|
||||
'authorization': 'Bearer ' + token,
|
||||
'cache-control': 'no-cache',
|
||||
'content-type': 'application/json',
|
||||
'origin': 'https://chat.forefront.ai',
|
||||
'pragma': 'no-cache',
|
||||
'referer': 'https://chat.forefront.ai/',
|
||||
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
|
||||
'sec-ch-ua-mobile': '?0',
|
||||
'sec-ch-ua-platform': '"macOS"',
|
||||
'sec-fetch-dest': 'empty',
|
||||
'sec-fetch-mode': 'cors',
|
||||
'sec-fetch-site': 'cross-site',
|
||||
'user-agent': UserAgent().random,
|
||||
}
|
||||
|
||||
json_data = {
|
||||
'text': prompt,
|
||||
'action': action_type,
|
||||
'parentId': chat_id,
|
||||
'workspaceId': chat_id,
|
||||
'messagePersona': default_persona,
|
||||
'model': model,
|
||||
}
|
||||
|
||||
for chunk in post(
|
||||
'https://chat-server.tenant-forefront-default.knative.chi.coreweave.com/chat',
|
||||
headers=headers,
|
||||
json=json_data,
|
||||
stream=True,
|
||||
).iter_lines():
|
||||
if b'finish_reason":null' in chunk:
|
||||
data = loads(chunk.decode('utf-8').split('data: ')[1])
|
||||
token = data['choices'][0]['delta'].get('content')
|
||||
|
||||
if token is not None:
|
||||
yield ForeFrontResponse(
|
||||
**{
|
||||
'id': chat_id,
|
||||
'object': 'text_completion',
|
||||
'created': int(time()),
|
||||
'text': token,
|
||||
'model': model,
|
||||
'choices': [{'text': token, 'index': 0, 'logprobs': None, 'finish_reason': 'stop'}],
|
||||
'usage': {
|
||||
'prompt_tokens': len(prompt),
|
||||
'completion_tokens': len(token),
|
||||
'total_tokens': len(prompt) + len(token),
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class Completion:
|
||||
@staticmethod
|
||||
def create(
|
||||
token=None,
|
||||
chat_id=None,
|
||||
prompt='',
|
||||
action_type='new',
|
||||
default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default
|
||||
model='gpt-4',
|
||||
) -> ForeFrontResponse:
|
||||
text = ''
|
||||
final_response = None
|
||||
for response in StreamingCompletion.create(
|
||||
token=token,
|
||||
chat_id=chat_id,
|
||||
prompt=prompt,
|
||||
action_type=action_type,
|
||||
default_persona=default_persona,
|
||||
model=model,
|
||||
):
|
||||
if response:
|
||||
final_response = response
|
||||
text += response.text
|
||||
|
||||
if final_response:
|
||||
final_response.text = text
|
||||
else:
|
||||
raise Exception('Unable to get the response, Please try again')
|
||||
|
||||
return final_response
|
@ -0,0 +1,26 @@
|
||||
from typing import Any, List
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Choice(BaseModel):
|
||||
text: str
|
||||
index: int
|
||||
logprobs: Any
|
||||
finish_reason: str
|
||||
|
||||
|
||||
class Usage(BaseModel):
|
||||
prompt_tokens: int
|
||||
completion_tokens: int
|
||||
total_tokens: int
|
||||
|
||||
|
||||
class ForeFrontResponse(BaseModel):
|
||||
id: str
|
||||
object: str
|
||||
created: int
|
||||
model: str
|
||||
choices: List[Choice]
|
||||
usage: Usage
|
||||
text: str
|
@ -1,9 +1,8 @@
|
||||
### Example: `theb` (use like openai pypi package) <a name="example-theb"></a>
|
||||
|
||||
|
||||
```python
|
||||
# import library
|
||||
import theb
|
||||
from gpt4free import theb
|
||||
|
||||
# simple streaming completion
|
||||
for token in theb.Completion.create('hello world'):
|
@ -1,4 +1,4 @@
|
||||
import theb
|
||||
|
||||
for token in theb.Completion.create('hello world'):
|
||||
print(token, end='', flush=True)
|
||||
print(token, end='', flush=True)
|
@ -1,7 +1,8 @@
|
||||
### Example: `you` (use like openai pypi package) <a name="example-you"></a>
|
||||
|
||||
```python
|
||||
import you
|
||||
|
||||
from gpt4free import you
|
||||
|
||||
# simple request with links and details
|
||||
response = you.Completion.create(
|
@ -1,4 +1,4 @@
|
||||
import theb
|
||||
from gpt4free import theb
|
||||
|
||||
for token in theb.Completion.create('hello world'):
|
||||
print(token, end='', flush=True)
|
||||
|
@ -1,24 +1,27 @@
|
||||
from openai_rev import openai_rev, Provider, quora, forefront
|
||||
import gpt4free
|
||||
from gpt4free import Provider, quora, forefront
|
||||
|
||||
# usage You
|
||||
response = openai_rev.Completion.create(Provider.You, prompt='Write a poem on Lionel Messi')
|
||||
response = gpt4free.Completion.create(Provider.You, prompt='Write a poem on Lionel Messi')
|
||||
print(response)
|
||||
|
||||
# usage Poe
|
||||
token = quora.Account.create(logging=False)
|
||||
response = openai_rev.Completion.create(
|
||||
Provider.Poe, prompt='Write a poem on Lionel Messi', token=token, model='ChatGPT'
|
||||
)
|
||||
response = gpt4free.Completion.create(Provider.Poe, prompt='Write a poem on Lionel Messi', token=token, model='ChatGPT')
|
||||
print(response)
|
||||
|
||||
# usage forefront
|
||||
token = forefront.Account.create(logging=False)
|
||||
response = openai_rev.Completion.create(
|
||||
response = gpt4free.Completion.create(
|
||||
Provider.ForeFront, prompt='Write a poem on Lionel Messi', model='gpt-4', token=token
|
||||
)
|
||||
print(response)
|
||||
print(f'END')
|
||||
|
||||
# usage theb
|
||||
response = openai_rev.Completion.create(Provider.Theb, prompt='Write a poem on Lionel Messi')
|
||||
response = gpt4free.Completion.create(Provider.Theb, prompt='Write a poem on Lionel Messi')
|
||||
print(response)
|
||||
|
||||
# usage cocalc
|
||||
response = gpt4free.Completion.create(Provider.CoCalc, prompt='Write a poem on Lionel Messi', cookie_input='')
|
||||
print(response)
|
||||
|
Loading…
Reference in New Issue