該項目為由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> (獲取)
回覆: