diff options
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | g4f/Provider/DeepAi.py | 1 | ||||
-rw-r--r-- | g4f/__init__.py | 4 | ||||
-rw-r--r-- | interference/app.py | 169 | ||||
-rw-r--r-- | setup.py | 4 | ||||
-rw-r--r-- | testing/test_providers.py | 3 |
6 files changed, 95 insertions, 90 deletions
@@ -4,7 +4,7 @@ By using this repository or any code related to it, you agree to the [legal noti ### New - official website: *https://g4f.ai* -- latest pypi version: ([0.1.4.4](https://pypi.org/project/g4f/0.1.4.4)): +- latest pypi version: ([0.1.4.6](https://pypi.org/project/g4f/0.1.4.6)): ```sh pip install -U g4f ``` @@ -573,4 +573,4 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. <a href="https://github.com/xtekky/gpt4free/stargazers"> <img width="500" alt="Star History Chart" src="https://api.star-history.com/svg?repos=xtekky/gpt4free&type=Date"> -</a> +</a>
\ No newline at end of file diff --git a/g4f/Provider/DeepAi.py b/g4f/Provider/DeepAi.py index 676f901e..7239f6ac 100644 --- a/g4f/Provider/DeepAi.py +++ b/g4f/Provider/DeepAi.py @@ -52,6 +52,7 @@ f = function () { headers = { "api-key": api_key, "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36", + **kwargs.get("headers", {}) } async with ClientSession( headers=headers diff --git a/g4f/__init__.py b/g4f/__init__.py index 01481f16..cd900e77 100644 --- a/g4f/__init__.py +++ b/g4f/__init__.py @@ -5,7 +5,7 @@ from .typing import Any, CreateResult, Union from requests import get logging = False -version = '0.1.4.4' +version = '0.1.4.6' def check_pypi_version(): try: @@ -104,4 +104,4 @@ class Completion: result = provider.create_completion(model.name, [{"role": "user", "content": prompt}], stream, **kwargs) - return result if stream else ''.join(result)
\ No newline at end of file + return result if stream else ''.join(result) diff --git a/interference/app.py b/interference/app.py index f25785f6..5abbcff2 100644 --- a/interference/app.py +++ b/interference/app.py @@ -1,104 +1,106 @@ import json +import time import random import string -import time -from typing import Any import requests -from flask import Flask, request -from flask_cors import CORS + +from typing import Any +from flask import Flask, request +from flask_cors import CORS from transformers import AutoTokenizer -from g4f import ChatCompletion +from g4f import ChatCompletion app = Flask(__name__) CORS(app) - -@app.route("/chat/completions", methods=["POST"]) +@app.route('/chat/completions', methods=['POST']) def chat_completions(): - model = request.get_json().get("model", "gpt-3.5-turbo") - stream = request.get_json().get("stream", False) - messages = request.get_json().get("messages") + model = request.get_json().get('model', 'gpt-3.5-turbo') + stream = request.get_json().get('stream', False) + messages = request.get_json().get('messages') - response = ChatCompletion.create(model=model, stream=stream, messages=messages) + response = ChatCompletion.create(model = model, + stream = stream, messages = messages) - completion_id = "".join(random.choices(string.ascii_letters + string.digits, k=28)) + completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28)) completion_timestamp = int(time.time()) if not stream: return { - "id": f"chatcmpl-{completion_id}", - "object": "chat.completion", - "created": completion_timestamp, - "model": model, - "choices": [ + 'id': f'chatcmpl-{completion_id}', + 'object': 'chat.completion', + 'created': completion_timestamp, + 'model': model, + 'choices': [ { - "index": 0, - "message": { - "role": "assistant", - "content": response, + 'index': 0, + 'message': { + 'role': 'assistant', + 'content': response, }, - "finish_reason": "stop", + 'finish_reason': 'stop', } ], - "usage": { - "prompt_tokens": None, - "completion_tokens": None, - "total_tokens": None, + 'usage': { + 'prompt_tokens': None, + 'completion_tokens': None, + 'total_tokens': None, }, } def streaming(): for chunk in response: completion_data = { - "id": f"chatcmpl-{completion_id}", - "object": "chat.completion.chunk", - "created": completion_timestamp, - "model": model, - "choices": [ + 'id': f'chatcmpl-{completion_id}', + 'object': 'chat.completion.chunk', + 'created': completion_timestamp, + 'model': model, + 'choices': [ { - "index": 0, - "delta": { - "content": chunk, + 'index': 0, + 'delta': { + 'content': chunk, }, - "finish_reason": None, + 'finish_reason': None, } ], } - content = json.dumps(completion_data, separators=(",", ":")) - yield f"data: {content}\n\n" + content = json.dumps(completion_data, separators=(',', ':')) + yield f'data: {content}\n\n' time.sleep(0.1) end_completion_data: dict[str, Any] = { - "id": f"chatcmpl-{completion_id}", - "object": "chat.completion.chunk", - "created": completion_timestamp, - "model": model, - "choices": [ + 'id': f'chatcmpl-{completion_id}', + 'object': 'chat.completion.chunk', + 'created': completion_timestamp, + 'model': model, + 'choices': [ { - "index": 0, - "delta": {}, - "finish_reason": "stop", + 'index': 0, + 'delta': {}, + 'finish_reason': 'stop', } ], } - content = json.dumps(end_completion_data, separators=(",", ":")) - yield f"data: {content}\n\n" + content = json.dumps(end_completion_data, separators=(',', ':')) + yield f'data: {content}\n\n' - return app.response_class(streaming(), mimetype="text/event-stream") + return app.response_class(streaming(), mimetype='text/event-stream') -#Get the embedding from huggingface +# Get the embedding from huggingface def get_embedding(input_text, token): huggingface_token = token - embedding_model = "sentence-transformers/all-mpnet-base-v2" + embedding_model = 'sentence-transformers/all-mpnet-base-v2' max_token_length = 500 - # Load the tokenizer for the "all-mpnet-base-v2" model + # Load the tokenizer for the 'all-mpnet-base-v2' model tokenizer = AutoTokenizer.from_pretrained(embedding_model) # Tokenize the text and split the tokens into chunks of 500 tokens each tokens = tokenizer.tokenize(input_text) - token_chunks = [tokens[i:i + max_token_length] for i in range(0, len(tokens), max_token_length)] + token_chunks = [tokens[i:i + max_token_length] + for i in range(0, len(tokens), max_token_length)] # Initialize an empty list embeddings = [] @@ -109,52 +111,53 @@ def get_embedding(input_text, token): chunk_text = tokenizer.convert_tokens_to_string(chunk) # Use the Hugging Face API to get embeddings for the chunk - api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{embedding_model}" - headers = {"Authorization": f"Bearer {huggingface_token}"} - chunk_text = chunk_text.replace("\n", " ") - + api_url = f'https://api-inference.huggingface.co/pipeline/feature-extraction/{embedding_model}' + headers = {'Authorization': f'Bearer {huggingface_token}'} + chunk_text = chunk_text.replace('\n', ' ') + # Make a POST request to get the chunk's embedding - response = requests.post(api_url, headers=headers, json={"inputs": chunk_text, "options": {"wait_for_model": True}}) - + response = requests.post(api_url, headers=headers, json={ + 'inputs': chunk_text, 'options': {'wait_for_model': True}}) + # Parse the response and extract the embedding chunk_embedding = response.json() # Append the embedding to the list embeddings.append(chunk_embedding) - #averaging all the embeddings - #this isn't very effective - #someone a better idea? + # averaging all the embeddings + # this isn't very effective + # someone a better idea? num_embeddings = len(embeddings) average_embedding = [sum(x) / num_embeddings for x in zip(*embeddings)] embedding = average_embedding return embedding -@app.route("/embeddings", methods=["POST"]) +@app.route('/embeddings', methods=['POST']) def embeddings(): - input_text_list = request.get_json().get("input") - input_text = ' '.join(map(str, input_text_list)) - token = request.headers.get('Authorization').replace("Bearer ", "") - embedding = get_embedding(input_text, token) + input_text_list = request.get_json().get('input') + input_text = ' '.join(map(str, input_text_list)) + token = request.headers.get('Authorization').replace('Bearer ', '') + embedding = get_embedding(input_text, token) + return { - "data": [ - { - "embedding": embedding, - "index": 0, - "object": "embedding" - } - ], - "model": "text-embedding-ada-002", - "object": "list", - "usage": { - "prompt_tokens": None, - "total_tokens": None - } - } + 'data': [ + { + 'embedding': embedding, + 'index': 0, + 'object': 'embedding' + } + ], + 'model': 'text-embedding-ada-002', + 'object': 'list', + 'usage': { + 'prompt_tokens': None, + 'total_tokens': None + } + } def main(): - app.run(host="0.0.0.0", port=1337, debug=True) - + app.run(host='0.0.0.0', port=1337, debug=True) -if __name__ == "__main__": +if __name__ == '__main__': main()
\ No newline at end of file @@ -14,7 +14,7 @@ with open("requirements.txt") as f: with open("interference/requirements.txt") as f: api_required = f.read().splitlines() -VERSION = '0.1.4.4' +VERSION = '0.1.4.6' DESCRIPTION = ( "The official gpt4free repository | various collection of powerful language models" ) @@ -73,4 +73,4 @@ setup( "Operating System :: MacOS :: MacOS X", "Operating System :: Microsoft :: Windows", ], -) +)
\ No newline at end of file diff --git a/testing/test_providers.py b/testing/test_providers.py index 462f19bc..ec0e0271 100644 --- a/testing/test_providers.py +++ b/testing/test_providers.py @@ -62,4 +62,5 @@ def test(_provider: type[BaseProvider]) -> bool: if __name__ == "__main__": - main()
\ No newline at end of file + main() +
\ No newline at end of file |