PHhTTPS commited on
Commit
960140f
·
1 Parent(s): 37a7ba4

Restoring missing FreeAPIs module and proxy files, enforcing ports

Browse files
freeapis-proxy/requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ flask
2
+ requests
3
+ g4f
freeapis-proxy/server.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import os
3
+ import logging
4
+
5
+ app = Flask(__name__)
6
+ logging.basicConfig(level=logging.DEBUG)
7
+
8
+ @app.route('/health', methods=['GET'])
9
+ def health():
10
+ return jsonify({"status": "ok", "service": "freeapis-proxy"})
11
+
12
+ @app.route('/v1/chat/completions', methods=['POST'])
13
+ def chat_completions():
14
+ # Placeholder for actual g4f/freeapis integration
15
+ # You would import g4f here and call it
16
+ return jsonify({
17
+ "id": "chatcmpl-freeapis",
18
+ "object": "chat.completion",
19
+ "created": 1234567890,
20
+ "model": "gpt-3.5-turbo",
21
+ "choices": [{
22
+ "index": 0,
23
+ "message": {
24
+ "role": "assistant",
25
+ "content": "This is a response from the Free APIs Proxy (Placeholder)."
26
+ },
27
+ "finish_reason": "stop"
28
+ }]
29
+ })
30
+
31
+ if __name__ == '__main__':
32
+ port = int(os.environ.get("PORT", 5001))
33
+ app.run(host='0.0.0.0', port=port)
internal/api/modules/freeapis/freeapis.go ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package freeapis
2
+
3
+ import (
4
+ "bytes"
5
+ "io"
6
+ "net/http"
7
+ "net/url"
8
+ "strings"
9
+
10
+ "github.com/gin-gonic/gin"
11
+ log "github.com/sirupsen/logrus"
12
+ )
13
+
14
+ type FreeAPIsModule struct {
15
+ Enabled bool
16
+ ProxyURL string
17
+ }
18
+
19
+ func NewFreeAPIsModule(enabled bool, proxyURL string) *FreeAPIsModule {
20
+ return &FreeAPIsModule{
21
+ Enabled: enabled,
22
+ ProxyURL: strings.TrimRight(proxyURL, "/"),
23
+ }
24
+ }
25
+
26
+ func (m *FreeAPIsModule) RegisterRoutes(engine *gin.Engine) {
27
+ if !m.Enabled {
28
+ return
29
+ }
30
+
31
+ // Route everything under /v1/freeapis/* to the python proxy
32
+ engine.Any("/v1/freeapis/*path", m.handleProxy)
33
+ }
34
+
35
+ func (m *FreeAPIsModule) handleProxy(c *gin.Context) {
36
+ // Strip /v1/freeapis prefix to get the target path
37
+ // e.g. /v1/freeapis/chat/completions -> /chat/completions (or however the python proxy expects it)
38
+ // Assuming python proxy expects OpenAI compatible paths directly under root or /v1
39
+
40
+ path := c.Param("path")
41
+ targetURL := m.ProxyURL + "/v1" + path
42
+
43
+ // If the proxy expects just /chat/completions, adjust accordingly.
44
+ // For now, let's assume it mimics OpenAI structure.
45
+
46
+ log.Debugf("Proxying FreeAPI request to: %s", targetURL)
47
+
48
+ req, err := http.NewRequest(c.Request.Method, targetURL, c.Request.Body)
49
+ if err != nil {
50
+ c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create request"})
51
+ return
52
+ }
53
+
54
+ // Copy headers
55
+ for k, v := range c.Request.Header {
56
+ req.Header[k] = v
57
+ }
58
+
59
+ client := &http.Client{}
60
+ resp, err := client.Do(req)
61
+ if err != nil {
62
+ log.Errorf("FreeAPIs proxy error: %v", err)
63
+ c.JSON(http.StatusBadGateway, gin.H{"error": "upstream proxy failed"})
64
+ return
65
+ }
66
+ defer resp.Body.Close()
67
+
68
+ // Copy response headers
69
+ for k, v := range resp.Header {
70
+ c.Header(k, v[0])
71
+ }
72
+ c.Status(resp.StatusCode)
73
+ io.Copy(c.Writer, resp.Body)
74
+ }