summaryrefslogtreecommitdiffstats
path: root/g4f/interference/__init__.py
diff options
context:
space:
mode:
authorCommenter123321 <36051603+Commenter123321@users.noreply.github.com>2023-10-09 18:02:06 +0200
committerCommenter123321 <36051603+Commenter123321@users.noreply.github.com>2023-10-09 18:02:06 +0200
commit119817c96349807efaf87ee432ce46446542b66a (patch)
tree1dbdf4d4dbf4f6c8a8247274ef500a2f1de765d1 /g4f/interference/__init__.py
parentaivvm's no life creator keeps patching it, but I'm just better 😉 (diff)
parentMerge branch 'main' of https://github.com/xtekky/gpt4free (diff)
downloadgpt4free-119817c96349807efaf87ee432ce46446542b66a.tar
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.gz
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.bz2
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.lz
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.xz
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.zst
gpt4free-119817c96349807efaf87ee432ce46446542b66a.zip
Diffstat (limited to 'g4f/interference/__init__.py')
-rw-r--r--g4f/interference/__init__.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/g4f/interference/__init__.py b/g4f/interference/__init__.py
new file mode 100644
index 00000000..d756faa7
--- /dev/null
+++ b/g4f/interference/__init__.py
@@ -0,0 +1,94 @@
+import json
+import time
+import random
+import string
+
+from typing import Any
+from flask import Flask, request
+from flask_cors import CORS
+from g4f import ChatCompletion
+
+app = Flask(__name__)
+CORS(app)
+
+@app.route('/')
+def index():
+ return 'interference api, url: http://127.0.0.1:1337'
+
+@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')
+
+ response = ChatCompletion.create(model = model,
+ stream = stream, messages = messages)
+
+ 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': [
+ {
+ 'index': 0,
+ 'message': {
+ 'role': 'assistant',
+ 'content': response,
+ },
+ 'finish_reason': 'stop',
+ }
+ ],
+ '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': [
+ {
+ 'index': 0,
+ 'delta': {
+ 'content': chunk,
+ },
+ 'finish_reason': None,
+ }
+ ],
+ }
+
+ 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': [
+ {
+ 'index': 0,
+ 'delta': {},
+ 'finish_reason': 'stop',
+ }
+ ],
+ }
+ content = json.dumps(end_completion_data, separators=(',', ':'))
+ yield f'data: {content}\n\n'
+
+ return app.response_class(streaming(), mimetype='text/event-stream')
+
+def run_interference():
+ app.run(host='0.0.0.0', port=1337, debug=True) \ No newline at end of file