This Flask-based project offers APIs a
Project Overview #
This project is a Flask-based web application that provides API interfaces and a simple frontend for interacting with GPT models. Users can interact with AI through a WeChat official account or a web interface to chat, generate images, or obtain content summaries.
Key features include:
- Text-based interaction with GPT models: Supports multi-turn conversations, simulating human-like chats.
- Image generation based on user input: Generates high-quality images using AI models.
- WeChat official account message handling: Supports auto-replies, image generation, and other features.
- Content summarization: Extracts key information from long texts or web pages to generate concise summaries.
This project is suitable for individual developers, enterprises, or researchers to quickly build applications based on GPT models.
Usage Instructions #
Environment Setup #
Clone the repository:
git clone <repository-url> cd gpt
Install dependencies:
pip install -r requirements.txt
Configure environment variables:
- Create a
.env
file and set the following variables:WX_TOKEN
: Token for the WeChat official account, used for message verification.OPENAI_API_KEY
: API key for the GPT model (if required).
- Ensure other necessary environment variables are correctly configured, such as database connection information (if applicable).
- Create a
Start the service:
python -m api.index
Deploy to Vercel:
- Ensure the
vercel.json
configuration is correct, including routes and environment variables. - Deploy using the Vercel CLI:
vercel
- Ensure the
API Usage #
Chat Interface:
- Path:
/g4f/<model>
- Method: POST
- Parameters:
message
: User input text.context
(optional): Conversation context.
- Response: Reply generated by the GPT model.
- Path:
Image Generation:
- Path:
/generate-image
- Method: POST
- Parameters:
prompt
: Text describing the content of the image.
- Response: URL of the generated image.
- Path:
WeChat Official Account:
- Path:
/wechat
- Method: POST
- Functionality: Handles messages from the WeChat official account, including text replies and image generation.
- Path:
Content Summarization:
- Path:
/ai-post
- Method: POST
- Parameters:
url
: URL of the webpage to summarize.
- Response: A concise summary of the webpage content.
- Path:
Project Structure #
api/
: Backend API code.static/
: Frontend static files (HTML, CSS, JS).templates/
: Frontend template files.requirements.txt
: Python dependency list.vercel.json
: Vercel deployment configuration.
Core Principles #
GPT Model Interaction:
- Uses the
g4f
client to interact with GPT models, supporting various models (e.g.,gpt-4o-mini
). - Provides flexible interfaces, supporting custom contexts and model selection.
- Uses the
WeChat Official Account Integration:
- Uses the
wechatpy
library to handle message signature verification, message parsing, and replies. - Supports text messages, image generation requests, and other custom features.
- Uses the
Image Generation:
- Calls the image generation interface of the
g4f
client to generate images based on user input prompts. - Returns the URL of the image for user download or preview.
- Calls the image generation interface of the
Content Summarization:
- Retrieves webpage content from the specified URL and generates a concise summary using the GPT model.
- Supports multilingual content processing.
Frequently Asked Questions #
How to switch GPT models?
- Call the
/models
interface to get a list of supported models. - Specify the desired model name in the request.
- Call the
How to debug WeChat official account features?
- Use the WeChat official platform’s developer tools for testing.
- Check if the
WX_TOKEN
is correctly configured.
What to do if image generation fails?
- Ensure the
g4f
client is working correctly. - Check if the input prompt meets the requirements.
- Ensure the