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

XFEstudio/gpt4free

~ | new folder inluding `./tool`and `./testing`

fdd8ef1f
abc <98614666+xtekky@users.noreply.github.com>
提交于

代码差异

12 个文件 +168 -0
Added etc/interference/app.py +163 -0
@@ -0,0 +1,163 @@
1 import json
2 import time
3 import random
4 import string
5 import requests
6
7 from typing import Any
8 from flask import Flask, request
9 from flask_cors import CORS
10 from transformers import AutoTokenizer
11 from g4f import ChatCompletion
12
13 app = Flask(__name__)
14 CORS(app)
15
16 @app.route('/chat/completions', methods=['POST'])
17 def chat_completions():
18 model = request.get_json().get('model', 'gpt-3.5-turbo')
19 stream = request.get_json().get('stream', False)
20 messages = request.get_json().get('messages')
21
22 response = ChatCompletion.create(model = model,
23 stream = stream, messages = messages)
24
25 completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28))
26 completion_timestamp = int(time.time())
27
28 if not stream:
29 return {
30 'id': f'chatcmpl-{completion_id}',
31 'object': 'chat.completion',
32 'created': completion_timestamp,
33 'model': model,
34 'choices': [
35 {
36 'index': 0,
37 'message': {
38 'role': 'assistant',
39 'content': response,
40 },
41 'finish_reason': 'stop',
42 }
43 ],
44 'usage': {
45 'prompt_tokens': None,
46 'completion_tokens': None,
47 'total_tokens': None,
48 },
49 }
50
51 def streaming():
52 for chunk in response:
53 completion_data = {
54 'id': f'chatcmpl-{completion_id}',
55 'object': 'chat.completion.chunk',
56 'created': completion_timestamp,
57 'model': model,
58 'choices': [
59 {
60 'index': 0,
61 'delta': {
62 'content': chunk,
63 },
64 'finish_reason': None,
65 }
66 ],
67 }
68
69 content = json.dumps(completion_data, separators=(',', ':'))
70 yield f'data: {content}\n\n'
71 time.sleep(0.1)
72
73 end_completion_data: dict[str, Any] = {
74 'id': f'chatcmpl-{completion_id}',
75 'object': 'chat.completion.chunk',
76 'created': completion_timestamp,
77 'model': model,
78 'choices': [
79 {
80 'index': 0,
81 'delta': {},
82 'finish_reason': 'stop',
83 }
84 ],
85 }
86 content = json.dumps(end_completion_data, separators=(',', ':'))
87 yield f'data: {content}\n\n'
88
89 return app.response_class(streaming(), mimetype='text/event-stream')
90
91
92 # Get the embedding from huggingface
93 def get_embedding(input_text, token):
94 huggingface_token = token
95 embedding_model = 'sentence-transformers/all-mpnet-base-v2'
96 max_token_length = 500
97
98 # Load the tokenizer for the 'all-mpnet-base-v2' model
99 tokenizer = AutoTokenizer.from_pretrained(embedding_model)
100 # Tokenize the text and split the tokens into chunks of 500 tokens each
101 tokens = tokenizer.tokenize(input_text)
102 token_chunks = [tokens[i:i + max_token_length]
103 for i in range(0, len(tokens), max_token_length)]
104
105 # Initialize an empty list
106 embeddings = []
107
108 # Create embeddings for each chunk
109 for chunk in token_chunks:
110 # Convert the chunk tokens back to text
111 chunk_text = tokenizer.convert_tokens_to_string(chunk)
112
113 # Use the Hugging Face API to get embeddings for the chunk
114 api_url = f'https://api-inference.huggingface.co/pipeline/feature-extraction/{embedding_model}'
115 headers = {'Authorization': f'Bearer {huggingface_token}'}
116 chunk_text = chunk_text.replace('\n', ' ')
117
118 # Make a POST request to get the chunk's embedding
119 response = requests.post(api_url, headers=headers, json={
120 'inputs': chunk_text, 'options': {'wait_for_model': True}})
121
122 # Parse the response and extract the embedding
123 chunk_embedding = response.json()
124 # Append the embedding to the list
125 embeddings.append(chunk_embedding)
126
127 # averaging all the embeddings
128 # this isn't very effective
129 # someone a better idea?
130 num_embeddings = len(embeddings)
131 average_embedding = [sum(x) / num_embeddings for x in zip(*embeddings)]
132 embedding = average_embedding
133 return embedding
134
135
136 @app.route('/embeddings', methods=['POST'])
137 def embeddings():
138 input_text_list = request.get_json().get('input')
139 input_text = ' '.join(map(str, input_text_list))
140 token = request.headers.get('Authorization').replace('Bearer ', '')
141 embedding = get_embedding(input_text, token)
142
143 return {
144 'data': [
145 {
146 'embedding': embedding,
147 'index': 0,
148 'object': 'embedding'
149 }
150 ],
151 'model': 'text-embedding-ada-002',
152 'object': 'list',
153 'usage': {
154 'prompt_tokens': None,
155 'total_tokens': None
156 }
157 }
158
159 def main():
160 app.run(host='0.0.0.0', port=1337, debug=True)
161
162 if __name__ == '__main__':
163 main()
Added etc/interference/requirements.txt +5 -0
@@ -0,0 +1,5 @@
1 flask_cors
2 watchdog~=3.0.0
3 transformers
4 tensorflow
5 torch
Renamed etc/testing/log_time.py +0 -0
此文件没有可显示的逐行差异。
Renamed etc/testing/test_async.py +0 -0
此文件没有可显示的逐行差异。
Renamed etc/testing/test_chat_completion.py +0 -0
此文件没有可显示的逐行差异。
Renamed etc/testing/test_interference.py +0 -0
此文件没有可显示的逐行差异。
Renamed etc/testing/test_needs_auth.py +0 -0
此文件没有可显示的逐行差异。
Renamed etc/testing/test_providers.py +0 -0
此文件没有可显示的逐行差异。
Renamed etc/tool/create_provider.py +0 -0
此文件没有可显示的逐行差异。
Renamed etc/tool/provider_init.py +0 -0
此文件没有可显示的逐行差异。
Renamed etc/tool/readme_table.py +0 -0
此文件没有可显示的逐行差异。
Renamed etc/tool/vercel.py +0 -0
此文件没有可显示的逐行差异。