Maximofn commited on
Commit
91a36bb
·
1 Parent(s): 30bf98b

feat(DOCUMENTATION): :book: Update README.md to enhance API documentation, including environment variables, deployment instructions, and error handling examples.

Browse files
Files changed (1) hide show
  1. README.md +255 -24
README.md CHANGED
@@ -11,78 +11,309 @@ short_description: LLM backend for IriusRisk Tech challenge
11
 
12
  # IriusRisk test challenge
13
 
14
- This project implements a FastAPI API that uses LangChain and LangGraph to generate text with the SmolLM2-1.7B-Instruct model from HuggingFace.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  ## Configuration
17
 
 
 
 
 
 
 
 
 
 
 
18
  ### In HuggingFace Spaces
19
 
20
  This project is designed to run in HuggingFace Spaces. To configure it:
21
 
22
- 1. Create a new Space in HuggingFace with SDK Docker
 
 
23
 
24
- ### Local development
25
 
26
- For local development:
27
 
28
  1. Clone this repository
 
29
  3. Install the dependencies:
30
- ```
31
  pip install -r requirements.txt
32
  ```
33
 
34
- ## Local execution
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  ```bash
37
  uvicorn app:app --reload
38
  ```
39
 
40
- The API will be available at `http://localhost:7860`.
 
 
 
 
 
 
 
 
 
41
 
42
  ## Endpoints
43
 
44
  ### GET `/`
45
 
46
  Welcome endpoint that returns a greeting message.
 
47
 
48
  ### POST `/generate`
49
 
50
  Endpoint to generate text using the language model.
51
-
52
- ### POST `/summarize`
53
-
54
- Endpoint to summarize text using the language model.
55
 
56
  **Request parameters:**
57
  ```json
58
  {
59
  "query": "Your question here",
60
- "thread_id": "optional_thread_identifier"
 
61
  }
62
  ```
63
 
64
- **Response:**
 
 
 
 
 
 
65
  ```json
66
  {
67
- "generated_text": "Generated text by the model",
68
- "thread_id": "thread identifier"
 
69
  }
70
  ```
71
 
72
- ## Docker
73
 
74
- To run the application in a Docker container:
75
 
 
76
  ```bash
77
- # Build the image
78
- docker build -t iriusrisk-test-challenge .
 
 
 
79
 
80
- # Run the container
81
- docker run -p 7860:7860 iriusrisk-test-challenge
 
 
 
82
  ```
83
 
84
- ## API documentation
 
 
 
 
 
 
 
 
 
 
85
 
86
  The interactive API documentation is available at:
87
- - Swagger UI: `http://localhost:7860/docs`
88
- - ReDoc: `http://localhost:7860/redoc`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  # IriusRisk test challenge
13
 
14
+ This project implements a FastAPI API that uses LangChain and LangGraph to generate text with the `SmolLM2-1.7B-Instruct` model from HuggingFace. I have chosen that model so that I could deploy it on a free GPU-only backend from Hugging Face for this test. The API includes security features such as API Key authentication and rate limiting to protect against abuse.
15
+
16
+ ## API URLs
17
+
18
+ - **Production**: `https://maximofn-iriusrisktestchallenge.hf.space`
19
+ - **Local Development**: `http://localhost:7860`
20
+
21
+ ## Main Features
22
+
23
+ - 🤖 Text generation using SmolLM2-1.7B-Instruct
24
+ - 📝 Text summarization capabilities
25
+ - 🔑 API Key authentication
26
+ - ⚡ Rate limiting for abuse protection
27
+ - 🔄 Conversation thread support
28
+ - 📚 Interactive documentation with Swagger and ReDoc
29
 
30
  ## Configuration
31
 
32
+ ### Environment Variables
33
+
34
+ For local deployment, create a `.env` file in the project root with the following variables:
35
+
36
+ ```env
37
+ API_KEY="your_secret_api_key"
38
+ ```
39
+
40
+ ## Deployment
41
+
42
  ### In HuggingFace Spaces
43
 
44
  This project is designed to run in HuggingFace Spaces. To configure it:
45
 
46
+ 1. Create a new Space in HuggingFace with blank Docker SDK
47
+ 2. Add all the files to the Space
48
+ 3. Configure the API_KEY in the Space's environment secrets
49
 
50
+ ### Local Docker Deployment
51
 
52
+ For local deployment:
53
 
54
  1. Clone this repository
55
+ 2. Create the `.env` file with your API_KEY
56
  3. Install the dependencies:
57
+ ```bash
58
  pip install -r requirements.txt
59
  ```
60
 
61
+ ### Local Docker Deployment
62
+
63
+ For local Docker deployment:
64
+
65
+ 1. Clone the repository
66
+ 2. Create the `.env` file with your API_KEY
67
+ 3. Build the Docker image:
68
+ ```bash
69
+ docker build -t iriusrisk-test-challenge .
70
+ ```
71
+ 4. Run the Docker container:
72
+ ```bash
73
+ docker run -p 8000:8000 --env-file .env iriusrisk-test-challenge
74
+ ```
75
+
76
+ ## Local Execution
77
 
78
  ```bash
79
  uvicorn app:app --reload
80
  ```
81
 
82
+ The API will be available at `http://localhost:8000`.
83
+
84
+ ## Local Docker Execution
85
+
86
+ ```bash
87
+ docker run -p 8000:8000 --env-file .env iriusrisk-test-challenge
88
+ ```
89
+
90
+ The API will be available at `http://localhost:8000`.
91
+
92
 
93
  ## Endpoints
94
 
95
  ### GET `/`
96
 
97
  Welcome endpoint that returns a greeting message.
98
+ - Rate limit: 10 requests per minute
99
 
100
  ### POST `/generate`
101
 
102
  Endpoint to generate text using the language model.
103
+ - Rate limit: 5 requests per minute
104
+ - Requires API Key authentication
 
 
105
 
106
  **Request parameters:**
107
  ```json
108
  {
109
  "query": "Your question here",
110
+ "thread_id": "optional_thread_identifier",
111
+ "system_prompt": "optional_system_prompt"
112
  }
113
  ```
114
 
115
+ ### POST `/summarize`
116
+
117
+ Endpoint to summarize text using the language model.
118
+ - Rate limit: 5 requests per minute
119
+ - Requires API Key authentication
120
+
121
+ **Request parameters:**
122
  ```json
123
  {
124
+ "text": "Text to summarize",
125
+ "thread_id": "optional_thread_identifier",
126
+ "max_length": 200
127
  }
128
  ```
129
 
130
+ ## Authentication
131
 
132
+ The API uses API Key authentication. You must include your API Key in the `X-API-Key` header for all protected endpoint requests.
133
 
134
+ Example:
135
  ```bash
136
+ # Production
137
+ curl -X POST "https://maximofn-iriusrisktestchallenge.hf.space/generate" \
138
+ -H "X-API-Key: your_api_key" \
139
+ -H "Content-Type: application/json" \
140
+ -d '{"query": "What is FastAPI?"}'
141
 
142
+ # Local development
143
+ curl -X POST "http://localhost:7860/generate" \
144
+ -H "X-API-Key: your_api_key" \
145
+ -H "Content-Type: application/json" \
146
+ -d '{"query": "What is FastAPI?"}'
147
  ```
148
 
149
+ ## Rate Limiting
150
+
151
+ To protect the API against abuse, the following limits have been implemented:
152
+
153
+ - Endpoint `/`: 10 requests per minute
154
+ - Endpoint `/generate`: 5 requests per minute
155
+ - Endpoint `/summarize`: 5 requests per minute
156
+
157
+ When these limits are exceeded, the API will return a 429 (Too Many Requests) error.
158
+
159
+ ## API Documentation
160
 
161
  The interactive API documentation is available at:
162
+ - Swagger UI:
163
+ - Production: `https://maximofn-iriusrisktestchallenge.hf.space/docs`
164
+ - Local: `http://localhost:7860/docs`
165
+ - ReDoc:
166
+ - Production: `https://maximofn-iriusrisktestchallenge.hf.space/redoc`
167
+ - Local: `http://localhost:7860/redoc`
168
+
169
+ ## Error Handling
170
+
171
+ The API includes error handling for the following situations:
172
+ - Error 401: API Key not provided
173
+ - Error 403: Invalid API Key
174
+ - Error 429: Rate limit exceeded
175
+ - Error 500: Internal server error
176
+
177
+ ## Code Examples
178
+
179
+ ### Python
180
+
181
+ Here are some examples of how to use the API with Python:
182
+
183
+ #### Text Generation
184
+
185
+ ```python
186
+ import requests
187
+
188
+ # API configuration
189
+ API_URL = "https://maximofn-iriusrisktestchallenge.hf.space" # Production URL
190
+ # API_URL = "http://localhost:7860" # Local development URL
191
+ API_KEY = "your_api_key" # Replace with your API key
192
+
193
+ # Headers for authentication
194
+ headers = {
195
+ "X-API-Key": API_KEY,
196
+ "Content-Type": "application/json"
197
+ }
198
+
199
+ # Generate text
200
+ def generate_text(query, thread_id="default", system_prompt=None):
201
+ url = f"{API_URL}/generate"
202
+
203
+ data = {
204
+ "query": query,
205
+ "thread_id": thread_id
206
+ }
207
+
208
+ # Add system prompt if provided
209
+ if system_prompt:
210
+ data["system_prompt"] = system_prompt
211
+
212
+ try:
213
+ response = requests.post(url, json=data, headers=headers)
214
+ if response.status_code == 200:
215
+ result = response.json()
216
+ return result["generated_text"]
217
+ else:
218
+ print(f"Error: {response.status_code}")
219
+ print(f"Details: {response.text}")
220
+ return None
221
+ except Exception as e:
222
+ print(f"Request failed: {str(e)}")
223
+ return None
224
+
225
+ # Example usage
226
+ query = "What are the main features of Python?"
227
+ result = generate_text(query)
228
+ if result:
229
+ print("Response:", result)
230
+
231
+ # Example with custom thread and system prompt
232
+ result = generate_text(
233
+ query="Explain object-oriented programming",
234
+ thread_id="programming_tutorial",
235
+ system_prompt="You are a programming teacher. Explain concepts in simple terms."
236
+ )
237
+ ```
238
+
239
+ #### Text Summarization
240
+
241
+ ```python
242
+ import requests
243
+
244
+ # Summarize text
245
+ def summarize_text(text, max_length=200, thread_id="default"):
246
+ url = f"{API_URL}/summarize"
247
+
248
+ data = {
249
+ "text": text,
250
+ "max_length": max_length,
251
+ "thread_id": thread_id
252
+ }
253
+
254
+ try:
255
+ response = requests.post(url, json=data, headers=headers)
256
+ if response.status_code == 200:
257
+ result = response.json()
258
+ return result["summary"]
259
+ else:
260
+ print(f"Error: {response.status_code}")
261
+ print(f"Details: {response.text}")
262
+ return None
263
+ except Exception as e:
264
+ print(f"Request failed: {str(e)}")
265
+ return None
266
+
267
+ # Example usage
268
+ text_to_summarize = """
269
+ Python is a high-level, interpreted programming language created by Guido van Rossum
270
+ and released in 1991. Python's design philosophy emphasizes code readability with
271
+ the use of significant whitespace. Its language constructs and object-oriented
272
+ approach aim to help programmers write clear, logical code for small and large-scale projects.
273
+ """
274
+
275
+ summary = summarize_text(text_to_summarize, max_length=50)
276
+ if summary:
277
+ print("Summary:", summary)
278
+ ```
279
+
280
+ #### Error Handling Example
281
+
282
+ ```python
283
+ def make_api_request(endpoint, data):
284
+ url = f"{API_URL}/{endpoint}"
285
+
286
+ try:
287
+ response = requests.post(url, json=data, headers=headers)
288
+
289
+ if response.status_code == 200:
290
+ return response.json()
291
+ elif response.status_code == 429:
292
+ print("Rate limit exceeded. Please wait before making more requests.")
293
+ elif response.status_code in (401, 403):
294
+ print("Authentication error. Please check your API key.")
295
+ else:
296
+ print(f"Error {response.status_code}: {response.text}")
297
+
298
+ return None
299
+ except requests.exceptions.ConnectionError:
300
+ print("Connection error. Please check if the API server is running.")
301
+ except Exception as e:
302
+ print(f"Unexpected error: {str(e)}")
303
+ return None
304
+ ```
305
+
306
+ These examples show how to:
307
+ - Make requests to different endpoints
308
+ - Handle authentication with API keys
309
+ - Process successful responses
310
+ - Handle various types of errors
311
+ - Use optional parameters like thread_id and system_prompt
312
+
313
+ Remember to:
314
+ - Replace `API_URL` with your actual API endpoint
315
+ - Set your API key in the headers
316
+ - Handle rate limiting by implementing appropriate delays between requests
317
+ - Implement proper error handling for your use case
318
+
319
+