Skip to content

Commit

Permalink
g4f fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
noes14155 committed Oct 9, 2023
1 parent 9512a00 commit e0a7369
Show file tree
Hide file tree
Showing 61 changed files with 80 additions and 4,884 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ Enable or disable plugins. Default value True.
Add your userid from telegram. If empty DM enable or disable option will be disabled.

- `GPT_KEY`
To use GPT4free GPT_KEY = '' (Only working in docker)
To use GPT4free GPT_KEY = ''
Key from the provider (including openai). Whichever api base you want use the key provided.
For Naga AI, Get this by messaging run the /key get command in th bot channel in [Naga AI Discord](https://discord.gg/JxRBXBhabu) , [Naga AI Telegram](https://t.me/chimer_ai)

- `API_BASE`
To use GPT4free API_BASE = 'http://g4f_server:1337' (Only working in docker)
To use GPT4free API_BASE = 'http://localhost:1337'
You can use any provider. I have included Naga AI api base. Use the key for the same.

- `MAX_HISTORY`
Expand Down
12 changes: 0 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,3 @@ services:
restart: always
# volumes:
# - ./home/personas:/app/personas
# depends_on:
# - g4f_server
# g4f_server:
# container_name: g4f_server
# ports:
# - '1337:1337'
# environment:
# - PYTHONUNBUFFERED=1
# build:
# context: ./interference
# dockerfile: Dockerfile
# restart: always
31 changes: 0 additions & 31 deletions interference/Dockerfile

This file was deleted.

136 changes: 70 additions & 66 deletions interference/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
from typing import Any
from flask import Flask, jsonify, request
from flask_cors import CORS
from transformers import AutoTokenizer
#from transformers import AutoTokenizer
import uvicorn
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('/models')
def get_models():
Expand Down Expand Up @@ -119,71 +122,72 @@ def streaming():


# Get the embedding from huggingface
def get_embedding(input_text, token):
huggingface_token = token
embedding_model = 'sentence-transformers/all-mpnet-base-v2'
max_token_length = 500

# 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)]

# Initialize an empty list
embeddings = []

# Create embeddings for each chunk
for chunk in token_chunks:
# Convert the chunk tokens back to text
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', ' ')

# 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}})

# 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?
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'])
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)

return {
'data': [
{
'embedding': embedding,
'index': 0,
'object': 'embedding'
}
],
'model': 'text-embedding-ada-002',
'object': 'list',
'usage': {
'prompt_tokens': None,
'total_tokens': None
}
}
#def get_embedding(input_text, token):
# huggingface_token = token
# embedding_model = 'sentence-transformers/all-mpnet-base-v2'
# max_token_length = 500
#
# # 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)]
#
# # Initialize an empty list
# embeddings = []
#
# # Create embeddings for each chunk
# for chunk in token_chunks:
# # Convert the chunk tokens back to text
# 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', ' ')
#
# # 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}})
#
# # 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?
# 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'])
#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)
#
# return {
# 'data': [
# {
# 'embedding': embedding,
# 'index': 0,
# 'object': 'embedding'
# }
# ],
# 'model': 'text-embedding-ada-002',
# 'object': 'list',
# 'usage': {
# 'prompt_tokens': None,
# 'total_tokens': None
# }
# }
#

def main():
asgi_app = WsgiToAsgi(app)
Expand Down
64 changes: 0 additions & 64 deletions interference/g4f/Provider/AItianhu.py

This file was deleted.

79 changes: 0 additions & 79 deletions interference/g4f/Provider/AItianhuSpace.py

This file was deleted.

Loading

0 comments on commit e0a7369

Please sign in to comment.