File size: 5,204 Bytes
d1a7225
 
 
8b706d4
 
 
 
 
d1a7225
 
 
8b706d4
d1a7225
8b706d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1a7225
16f7ae8
8b706d4
 
191a48e
 
8b706d4
 
 
191a48e
 
 
 
8b706d4
191a48e
8b706d4
 
 
191a48e
 
 
8b706d4
d1a7225
8b706d4
d1a7225
 
 
 
 
8b706d4
 
 
d5a5d22
d1a7225
 
8b706d4
d1a7225
8b706d4
d1a7225
8b706d4
 
 
 
d1a7225
d5a5d22
d1a7225
 
8b706d4
d1a7225
8b706d4
 
d1a7225
8b706d4
d1a7225
8b706d4
d1a7225
 
d5a5d22
8b706d4
 
 
 
 
 
 
 
d5a5d22
d1a7225
8b706d4
 
 
 
 
 
 
 
 
d1a7225
8b706d4
 
 
 
 
 
 
 
 
d1a7225
 
8b706d4
 
 
d1a7225
d5a5d22
8b706d4
d1a7225
8b706d4
 
 
 
d1a7225
8b706d4
 
 
 
 
 
 
d1a7225
8b706d4
d1a7225
8b706d4
 
d5a5d22
8b706d4
 
d5a5d22
8b706d4
d1a7225
 
8b706d4
 
 
 
d1a7225
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from fastapi import FastAPI, HTTPException, Depends, Header, Request
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from typing import List, Optional, Literal
import json
import g4f
from g4f.Provider import OpenaiAccount, RetryProvider
from g4f.models import ModelUtils

app = FastAPI()

# Complete list of available models from G4F
models = [
    # OpenAI models
    "gpt-4", "gpt-4-turbo", "gpt-4o", "gpt-3.5-turbo",
    
    # Anthropic models
    "claude-3-opus", "claude-3-sonnet", "claude-3-haiku", "claude-2.1",
    
    # Google models
    "gemini-pro", "gemini-1.5-pro", "gemini-1.5-flash",
    
    # Meta models
    "llama-2-70b", "llama-2-13b", "llama-2-7b", "llama-3-70b", "llama-3-8b",
    
    # Other providers
    "mistral-7b", "mixtral-8x7b", "command-r-plus", "cohere-command-r",
    "deepseek-chat", "deepseek-coder", "code-llama-34b", "code-llama-70b",
    
    # Specialized models
    "grok-1", "grok-1.5", "grok-2", "o1", "o3-mini", "flux", "flux-pro"
]

# Configure G4F backend
class CustomBackend(g4f.Provider.BackendApi):
    working = True
    ssl = False
    url = "https://ahe.hopto.org"
    headers = {"Authorization": "Basic Z2dnOmc0Zl8="}
    
    @classmethod
    async def create_async_generator(
        cls,
        model: str,
        messages: g4f.typing.Messages,
        **kwargs
    ) -> g4f.typing.AsyncResult:
        if model in OpenaiAccount.get_models():
            kwargs["provider"] = OpenaiAccount
        async for chunk in super().create_async_generator(model, messages, **kwargs):
            yield chunk

# Pydantic models
class Message(BaseModel):
    role: Literal["system", "user", "assistant"]
    content: str

class ChatRequest(BaseModel):
    model: str
    messages: List[Message]
    temperature: Optional[float] = 0.7
    max_tokens: Optional[int] = None
    top_p: Optional[float] = 0.9
    streaming: bool = True

class ChatResponse(BaseModel):
    role: str = "assistant"
    content: str
    model: Optional[str] = None

class ModelListResponse(BaseModel):
    models: List[str]

# API Key Verification
async def verify_api_key(x_api_key: str = Header(...)):
    if x_api_key != "fb207532285886a5568298b4b4e61124":
        raise HTTPException(status_code=403, detail="Invalid API key")

@app.get("/v1/models", response_model=ModelListResponse, tags=["Models"])
async def get_models():
    """Get list of all available models"""
    return ModelListResponse(models=models)

@app.post("/v1/chat/completions", response_model=Optional[ChatResponse], tags=["Chat"])
async def chat_completion(
    request: ChatRequest,
    api_key: str = Depends(verify_api_key)
):
    """
    Handle chat completion requests with streaming support.
    
    Args:
        request: ChatRequest containing model, messages and parameters
        api_key: Verified API key
        
    Returns:
        Either a StreamingResponse or direct ChatResponse
    """
    # Validate model
    if request.model not in models:
        raise HTTPException(
            status_code=400,
            detail=f"Invalid model. Available models: {', '.join(models)}"
        )

    # Prepare messages
    messages = [{"role": msg.role, "content": msg.content} for msg in request.messages]
    
    try:
        if request.streaming:
            async def stream_generator():
                response = await g4f.ChatCompletion.create_async(
                    model=request.model,
                    messages=messages,
                    temperature=request.temperature,
                    top_p=request.top_p,
                    max_tokens=request.max_tokens,
                    provider=RetryProvider([CustomBackend])
                )
                
                async for chunk in response:
                    if isinstance(chunk, dict):
                        yield f"data: {json.dumps(chunk)}\n\n"
                    else:
                        yield f"data: {json.dumps({'content': str(chunk)})}\n\n"
                yield "data: [DONE]\n\n"

            return StreamingResponse(
                stream_generator(),
                media_type="text/event-stream"
            )
        else:
            response = await g4f.ChatCompletion.create_async(
                model=request.model,
                messages=messages,
                temperature=request.temperature,
                top_p=request.top_p,
                max_tokens=request.max_tokens,
                provider=RetryProvider([CustomBackend])
            )
            
            if isinstance(response, str):
                return ChatResponse(content=response, model=request.model)
            elif isinstance(response, dict):
                return ChatResponse(
                    content=response.get("choices", [{}])[0].get("message", {}).get("content", ""),
                    model=request.model
                )
            return ChatResponse(content=str(response), model=request.model

    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Error processing request: {str(e)}"
        )

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)