mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-09-12 08:29:30 -04:00
Update __init__.py
This commit is contained in:
parent
318112c8b9
commit
0af4fc0997
@ -1,45 +1,41 @@
|
|||||||
from fastapi import FastAPI, Response, Request
|
from fastapi import FastAPI, Response, Request
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
|
||||||
from typing import List, Union, Any, Dict, AnyStr
|
from typing import List, Union, Any, Dict, AnyStr
|
||||||
from ._tokenizer import tokenize
|
from ._tokenizer import tokenize
|
||||||
import g4f
|
from .. import BaseProvider
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import uvicorn
|
import uvicorn
|
||||||
import nest_asyncio
|
import nest_asyncio
|
||||||
|
import g4f
|
||||||
|
|
||||||
app = FastAPI()
|
class Api:
|
||||||
nest_asyncio.apply()
|
def __init__(self, engine: g4f, debug: bool = True, sentry: bool = False,
|
||||||
|
list_ignored_providers: List[Union[str, BaseProvider]] = None) -> None:
|
||||||
|
self.engine = engine
|
||||||
|
self.debug = debug
|
||||||
|
self.sentry = sentry
|
||||||
|
self.list_ignored_providers = list_ignored_providers
|
||||||
|
|
||||||
origins = [
|
self.app = FastAPI()
|
||||||
"http://localhost",
|
nest_asyncio.apply()
|
||||||
"http://localhost:1337",
|
|
||||||
]
|
|
||||||
|
|
||||||
app.add_middleware(
|
JSONObject = Dict[AnyStr, Any]
|
||||||
CORSMiddleware,
|
JSONArray = List[Any]
|
||||||
allow_origins=origins,
|
JSONStructure = Union[JSONArray, JSONObject]
|
||||||
allow_credentials=True,
|
|
||||||
allow_methods=["*"],
|
|
||||||
allow_headers=["*"],
|
|
||||||
)
|
|
||||||
|
|
||||||
JSONObject = Dict[AnyStr, Any]
|
@self.app.get("/")
|
||||||
JSONArray = List[Any]
|
async def read_root():
|
||||||
JSONStructure = Union[JSONArray, JSONObject]
|
return Response(content=json.dumps({"info": "g4f API"}, indent=4), media_type="application/json")
|
||||||
|
|
||||||
@app.get("/")
|
@self.app.get("/v1")
|
||||||
async def read_root():
|
async def read_root_v1():
|
||||||
return Response(content=json.dumps({"info": "G4F API"}, indent=4), media_type="application/json")
|
|
||||||
|
|
||||||
@app.get("/v1")
|
|
||||||
async def read_root_v1():
|
|
||||||
return Response(content=json.dumps({"info": "Go to /v1/chat/completions or /v1/models."}, indent=4), media_type="application/json")
|
return Response(content=json.dumps({"info": "Go to /v1/chat/completions or /v1/models."}, indent=4), media_type="application/json")
|
||||||
|
|
||||||
@app.get("/v1/models")
|
@self.app.get("/v1/models")
|
||||||
async def models():
|
async def models():
|
||||||
model_list = [{
|
model_list = [{
|
||||||
'id': model,
|
'id': model,
|
||||||
'object': 'model',
|
'object': 'model',
|
||||||
@ -50,8 +46,8 @@ async def models():
|
|||||||
'object': 'list',
|
'object': 'list',
|
||||||
'data': model_list}, indent=4), media_type="application/json")
|
'data': model_list}, indent=4), media_type="application/json")
|
||||||
|
|
||||||
@app.get("/v1/models/{model_name}")
|
@self.app.get("/v1/models/{model_name}")
|
||||||
async def model_info(model_name: str):
|
async def model_info(model_name: str):
|
||||||
try:
|
try:
|
||||||
model_info = (g4f.ModelUtils.convert[model_name])
|
model_info = (g4f.ModelUtils.convert[model_name])
|
||||||
|
|
||||||
@ -64,9 +60,8 @@ async def model_info(model_name: str):
|
|||||||
except:
|
except:
|
||||||
return Response(content=json.dumps({"error": "The model does not exist."}, indent=4), media_type="application/json")
|
return Response(content=json.dumps({"error": "The model does not exist."}, indent=4), media_type="application/json")
|
||||||
|
|
||||||
@app.post("/v1/chat/completions")
|
@self.app.post("/v1/chat/completions")
|
||||||
async def chat_completions(request: Request, item: JSONStructure = None):
|
async def chat_completions(request: Request, item: JSONStructure = None):
|
||||||
|
|
||||||
item_data = {
|
item_data = {
|
||||||
'model': 'gpt-3.5-turbo',
|
'model': 'gpt-3.5-turbo',
|
||||||
'stream': False,
|
'stream': False,
|
||||||
@ -158,10 +153,10 @@ async def chat_completions(request: Request, item: JSONStructure = None):
|
|||||||
|
|
||||||
return Response(content=json.dumps(streaming(), indent=4), media_type="application/json")
|
return Response(content=json.dumps(streaming(), indent=4), media_type="application/json")
|
||||||
|
|
||||||
@app.post("/v1/completions")
|
@self.app.post("/v1/completions")
|
||||||
async def completions():
|
async def completions():
|
||||||
return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json")
|
return Response(content=json.dumps({'info': 'Not working yet.'}, indent=4), media_type="application/json")
|
||||||
|
|
||||||
def run(ip, thread_quantity):
|
def run(self, ip, thread_quantity):
|
||||||
split_ip = ip.split(":")
|
split_ip = ip.split(":")
|
||||||
uvicorn.run(app, host=split_ip[0], port=int(split_ip[1]), use_colors=False, workers=thread_quantity)
|
uvicorn.run(self.app, host=split_ip[0], port=int(split_ip[1]), use_colors=False, workers=thread_quantity)
|
||||||
|
Loading…
Reference in New Issue
Block a user