该项目为由Google AI的生成语言模型提供动力的提问系统提供了后端API。用户可以提交问题,接收AI模型生成的答案,并选择存储他们的问题和答案以供将来参考。
克隆这个存储库:
git clone [https://github.com/Faizgeeky/faiz-mohammad-answersai-backend.git](https://github.com/Faizgeeky/faiz-mohammad-answersai-backend.git)
导航到项目目录:
cd faiz-mohammad-answersai-backend
3-安装依赖项:
npm i
Configuration
# # 1. Environment Variables:
# Create a file named .env in your project's root directory. This file should contain the following environment variables (replace with your actual values):
access_token_secret = your_secret_key mongo_uri = mongodb:// your_mongo_connection_string gemini_api_key = your_google_cloud_api_key
# Important:
ACCESS_TOKEN_SECRET: A secret key used for signing JWT tokens for user authentication. Choose a strong and unique key.
MONGO_URI: The connection string for your MongoDB database.
GEMINI_API_KEY: Your Google Cloud API key for accessing the Generative AI service.
Note: You can exclude the .env file from version control using tools like .gitignore.
## 2. MongoDB Database:
Set up a MongoDB database instance and configure the MONGO_URI environment variable accordingly.
Running the Application
Start the development server:
NPM开始#或纱线开始
Use code with caution.
content_copy
This will start the Node.js server and listen for incoming requests on the default port (usually 8000).
(Optional) Start a live-reloading server for development:
NPM运行服务器#或纱线服务器
Use code with caution.
content_copy
This will use Nodemon to automatically restart the server whenever you make changes to your code.
API Usage
The API endpoints are documented below. You can use tools like Postman or curl to send requests to the server.
1. User Authentication:
# Endpoint: /api/users/ (POST)
json {“ username”:“ your_username”,“密码”:“ your_password”}
请求主体:
{
"username": "your_username",
"password": "your_password"
}
回复:
json {“ token”:“ your_jwt_token”}
成功登录后,服务器将返回一个JWT令牌,您需要在需要身份验证的后续请求中包含这些令牌。
端点: /API /问题(帖子)
请求标题:
授权:携带者<your_jwt_token>请求正文:
json {“ Question”:“ your_question”}谨慎使用代码。 content_copy响应:
json {“ _id”:“ Question_ID”,“ user_id”:“ user_id”,“ content”:“ your_question”,“答案”:“ ai_generated_answer”,“ createat”:“ timestamp”} timeStamp“} timeStamp”}使用谨慎使用代码。 content_copy 3。检索问题:
端点: /api/questions (获取)
回复:
[
{ ...question1 object ... },
{ ...question2 object ... },
...
]端点: /api/questions/user/<user_id> (获取)
回复: