English | 简体中文
Fitten Code API Server is a FastAPI-based application that provides API service for Fitten Code. It acts as a middleware between clients and the Fitten Code service, offering a standardized API interface similar to OpenAI's API format. This server handles authentication, token management, and provides a clean interface for applications to interact with Fitten Code's capabilities.
- Configuration management through config.ini
- Automatic token management (login, refresh)
- OpenAI-like API endpoints
- Support for streaming chat completion
- Built-in error handling and token expiration management
- Logging system for debugging and monitoring
- Secure API key authentication
- Support for 128k context length
- Python 3.x
- FastAPI
- Uvicorn
- httpx
- Pydantic
- configparser
-
Clone this repository
git clone https://github.com/bobotechnology/fitten-code-api.git cd fitten-code-api -
Install dependencies:
pip install -r requirements.txt
-
Create a
config.inifile in the project root directory with the following content:[Main] username=your_fittentech_username password=your_fittentech_password api_key=your_custom_api_key
Configuration parameters:
username: Your Fitten Code account usernamepassword: Your Fitten Code account passwordapi_key: Custom API key for client authentication (you can create your own secure key)
-
Start the server:
python api.py
The server will run on
http://localhost:5000by defaultAlternatively, you can use uvicorn directly:
uvicorn api:app --host 0.0.0.0 --port 5000
-
API Endpoints:
GET /v1/models: List available modelsPOST /v1/chat/completions: Chat completion endpoint (supports streaming responses)
-
Example API calls:
Regular Request:
import httpx import json url = "http://localhost:5000/v1/chat/completions" headers = { "Authorization": "Bearer your_api_key", "Content-Type": "application/json" } data = { "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"} ], "stream": False } response = httpx.post(url, headers=headers, json=data) print(json.dumps(response.json(), indent=2))
Streaming Request:
import httpx import json url = "http://localhost:5000/v1/chat/completions" headers = { "Authorization": "Bearer your_api_key", "Content-Type": "application/json" } data = { "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Write a short story."} ], "stream": True } with httpx.stream('POST', url, headers=headers, json=data, timeout=None) as response: for line in response.iter_lines(): if line: line_text = line.decode('utf-8') if line_text.startswith('data: '): data_str = line_text[6:] if data_str == "[DONE]": break try: chunk = json.loads(data_str) if 'choices' in chunk and len(chunk['choices']) > 0: content = chunk['choices'][0].get('delta', {}).get('content', '') if content: print(content, end='', flush=True) except json.JSONDecodeError: pass print() # Final newline
The server creates a log file fitten_api.log in the project directory that contains detailed information about server operations, API requests, and errors. This can be useful for debugging.
You can also customize the server port and host in the code or by using command line parameters with uvicorn.
The API server handles various error scenarios:
- Invalid API key authentication
- Token expiration and automatic refresh
- API request failures
- Rate limiting and server errors
This project is licensed under the MIT License - see the LICENSE.md file for details.