Spaces:
Sleeping
Sleeping
Anwar11234
commited on
Commit
•
48f8e91
1
Parent(s):
5bb6975
first commit
Browse files- .env +1 -0
- Dockerfile +14 -0
- examples.py +383 -0
- main.py +74 -0
- requirements.txt +7 -0
- system_message.py +49 -0
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
GROQ_API_KEY=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
|
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python 3.10.9 image
|
2 |
+
FROM python:3.10.9
|
3 |
+
|
4 |
+
# Copy the current directory contents into the container at .
|
5 |
+
COPY . .
|
6 |
+
|
7 |
+
# Set the working directory to /
|
8 |
+
WORKDIR /
|
9 |
+
|
10 |
+
# Install requirements.txt
|
11 |
+
RUN pip install --no-cache-dir -r /requirements.txt
|
12 |
+
|
13 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
14 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
examples.py
ADDED
@@ -0,0 +1,383 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
examples = [
|
2 |
+
{"input": "Explain the concept of recursion",
|
3 |
+
"output": """Recursion is a programming technique where a function calls itself directly or indirectly in order to solve a problem. The recursive function solves a small part of the problem and then calls itself to solve the smaller remaining problem. This continues until the function reaches a base case, which is a condition that stops the recursion.
|
4 |
+
|
5 |
+
To illustrate this concept, I will create a flowchart showing how recursion works with an example of calculating the factorial of a number. A flowchart is suitable here as it helps visualize the step-by-step process of recursion.
|
6 |
+
|
7 |
+
Example: Calculating Factorial
|
8 |
+
Factorial of n (n!) is the product of all positive integers up to n.
|
9 |
+
Recursive Definition:
|
10 |
+
Base Case: fact(1) = 1
|
11 |
+
Recursive Case: fact(n)=n*fact(n-1)
|
12 |
+
|
13 |
+
Flowchart:
|
14 |
+
```mermaid
|
15 |
+
graph TD
|
16 |
+
A["Main Program"] --> B["fact(4)"]
|
17 |
+
B --> C["fact(3)"]
|
18 |
+
C --> D["fact(2)"]
|
19 |
+
D --> E["fact(1)"]
|
20 |
+
E -- base case: \n return 1 --> D
|
21 |
+
D -- return 2 * 1 = 2 --> C
|
22 |
+
C -- return 3 * 2 = 6 --> B
|
23 |
+
B -- return 4 * 6 = 24 --> A
|
24 |
+
```
|
25 |
+
|
26 |
+
This diagram illustrates the recursive calculation of the factorial of 4. Here's a step-by-step explanation:
|
27 |
+
1. Main Program starts the calculation and calls fact(4).
|
28 |
+
2. fact(4) calls fact(3).
|
29 |
+
3. fact(3) calls fact(2).
|
30 |
+
4. fact(2) calls fact(1).
|
31 |
+
5. fact(1) hits the base case and returns 1.
|
32 |
+
6. The result of fact(1) is used by fact(2) to return 2.
|
33 |
+
7. The result of fact(2) is used by fact(3) to return 6.
|
34 |
+
8. The result of fact(3) is used by fact(4) to return 24."""},
|
35 |
+
|
36 |
+
{"input": "explain the binary search algorithm.",
|
37 |
+
"output": """
|
38 |
+
Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.
|
39 |
+
To visualize the algorithm's steps, a flowchart is most suitable.
|
40 |
+
```mermaid
|
41 |
+
graph TD
|
42 |
+
A["Start"] --> B["Initialize low = 0, high = n-1"]
|
43 |
+
B --> C["Calculate mid = (low + high) / 2"]
|
44 |
+
C --> D["Compare target with arr[mid]"]
|
45 |
+
D --> E["Target found at mid"]
|
46 |
+
D --> F["Target < arr[mid]"]
|
47 |
+
D --> G["Target > arr[mid]"]
|
48 |
+
F --> H["Update high = mid - 1"]
|
49 |
+
G --> I["Update low = mid + 1"]
|
50 |
+
H --> C
|
51 |
+
I --> C
|
52 |
+
E --> J["End"]
|
53 |
+
C --> K["low > high"]
|
54 |
+
K --> L["Target not found"]
|
55 |
+
L --> J
|
56 |
+
```
|
57 |
+
|
58 |
+
Here's a step-by-step explanation of the binary search algorithm:
|
59 |
+
|
60 |
+
1. Initialize `low` to 0 and `high` to `n-1`, where `n` is the length of the array.
|
61 |
+
2. Calculate the middle index `mid` using the formula `(low + high) / 2`.
|
62 |
+
3. Compare the target value with the value at `arr[mid]`.
|
63 |
+
4. If the target value is found at `arr[mid]`, return the index `mid`.
|
64 |
+
5. If the target value is less than `arr[mid]`, update `high` to `mid - 1`.
|
65 |
+
6. If the target value is greater than `arr[mid]`, update `low` to `mid + 1`.
|
66 |
+
7. Repeat steps 2-6 until `low` is greater than `high`.
|
67 |
+
8. If `low` is greater than `high`, the target value is not found in the array.
|
68 |
+
|
69 |
+
The binary search algorithm has a time complexity of O(log n), making it very efficient for large lists.
|
70 |
+
"""},
|
71 |
+
|
72 |
+
{"input": "What are the main principles of Object-Oriented Programming (OOP)?", "output": """
|
73 |
+
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects. These objects contain data and methods that manipulate that data. To explain the main principles of OOP, I will create a mind map, which is suitable for organizing concepts hierarchically.
|
74 |
+
|
75 |
+
Mind Map:
|
76 |
+
```mermaid
|
77 |
+
mindmap
|
78 |
+
root(("Object-Oriented Programming (OOP)"))
|
79 |
+
Encapsulation
|
80 |
+
Data Hiding
|
81 |
+
Access Modifiers
|
82 |
+
Private
|
83 |
+
Public
|
84 |
+
Protected
|
85 |
+
Abstraction
|
86 |
+
Simplification
|
87 |
+
Essential Features
|
88 |
+
Interface
|
89 |
+
Inheritance
|
90 |
+
Reusability
|
91 |
+
Base Class
|
92 |
+
Derived Class
|
93 |
+
Overriding
|
94 |
+
Polymorphism
|
95 |
+
Method Overloading
|
96 |
+
Method Overriding
|
97 |
+
Runtime Polymorphism
|
98 |
+
Compile-time Polymorphism
|
99 |
+
Classes and Objects
|
100 |
+
Class Definition
|
101 |
+
Object Instantiation
|
102 |
+
Attributes
|
103 |
+
Methods
|
104 |
+
Additional Principles
|
105 |
+
Composition
|
106 |
+
Aggregation
|
107 |
+
Association
|
108 |
+
|
109 |
+
```
|
110 |
+
This mind map provides a structured overview of the main principles and components of Object-Oriented Programming (OOP).
|
111 |
+
1. Encapsulation
|
112 |
+
- Data Hiding: Encapsulation involves hiding the internal state of objects and restricting access to it.
|
113 |
+
- Access Modifiers: Controls access levels to the attributes and methods of a class.
|
114 |
+
-- Private: Accessible only within the class.
|
115 |
+
-- Public: Accessible from any other code.
|
116 |
+
-- Protected: Accessible within the class and its subclasses.
|
117 |
+
2. Abstraction
|
118 |
+
- Simplification: Focuses on the essential qualities of an object rather than the specific characteristics.
|
119 |
+
- Essential Features: Provides only the necessary details and hides the complexity.
|
120 |
+
- Interface: Defines a contract for what a class can do without specifying how it does it.
|
121 |
+
3. Inheritance
|
122 |
+
- Reusability: Allows a new class to inherit properties and behaviors from an existing class.
|
123 |
+
- Base Class: The class whose properties are inherited.
|
124 |
+
- Derived Class: The class that inherits properties from the base class.
|
125 |
+
- Overriding: Allows a subclass to provide a specific implementation of a method already defined in its superclass.
|
126 |
+
4. Polymorphism
|
127 |
+
- Method Overloading: Allows multiple methods with the same name but different parameters within the same class.
|
128 |
+
- Method Overriding: Allows a subclass to provide a specific implementation of a method defined in its superclass.
|
129 |
+
- Runtime Polymorphism: Method overriding achieved during runtime.
|
130 |
+
- Compile-time Polymorphism: Method overloading achieved during compile-time.
|
131 |
+
5. Classes and Objects
|
132 |
+
- Class Definition: A blueprint for creating objects (a particular data structure).
|
133 |
+
- Object Instantiation: The creation of a real instance (object) from a class.
|
134 |
+
- Attributes: Characteristics or properties of a class.
|
135 |
+
- Methods: Functions or behaviors defined within a class.
|
136 |
+
6. Additional Principles
|
137 |
+
- Composition: A way to combine simple objects or data types into more complex ones.
|
138 |
+
- Aggregation: A specialized form of composition where the child can exist independently of the parent.
|
139 |
+
- Association: A relationship between two objects.
|
140 |
+
"""},
|
141 |
+
|
142 |
+
{"input": "what're middlewares in terms of backend development?" ,
|
143 |
+
"output": """In backend development, a middleware is a software component that mediates between two or more applications, services, or systems. It acts as an intermediary layer that enables communication, data exchange, and functionality between these entities.
|
144 |
+
|
145 |
+
To illustrate the concept of middleware, a sequence diagram is most suitable. It shows the flow of requests and responses between components.
|
146 |
+
|
147 |
+
Sequence Diagram:
|
148 |
+
```mermaid
|
149 |
+
sequenceDiagram
|
150 |
+
participant Client as "Client App"
|
151 |
+
participant Middleware as "Middleware"
|
152 |
+
participant Server as "Backend Server"
|
153 |
+
|
154 |
+
Client->>Middleware: Request
|
155 |
+
Middleware->>Middleware: Process request
|
156 |
+
Middleware->>Server: Forward request
|
157 |
+
Server->>Middleware: Response
|
158 |
+
Middleware->>Middleware: Process response
|
159 |
+
Middleware->>Client: Forward response
|
160 |
+
```
|
161 |
+
In this sequence diagram, the middleware acts as an intermediary between the client application and the backend server. It receives requests from the client, processes them, and then forwards them to the server. The server responds to the middleware, which then processes the response and forwards it to the client.
|
162 |
+
|
163 |
+
Middlewares can perform various tasks, such as:
|
164 |
+
|
165 |
+
1. Authentication and authorization
|
166 |
+
2. Request and response logging
|
167 |
+
3. Error handling and debugging
|
168 |
+
4. Data encryption and decryption
|
169 |
+
5. Rate limiting and throttling
|
170 |
+
6. Caching and content delivery
|
171 |
+
7. API gateway and routing
|
172 |
+
8. Message queuing and workflow management"""},
|
173 |
+
{"input": "Create a class diagram for a simple e-commerce system with classes for User, Product, Order, and Payment", "output": """
|
174 |
+
To model the relationships and structure of a simple e-commerce system, a class diagram is most appropriate. It allows us to visualize the classes, their attributes, methods, and relationships.
|
175 |
+
Class Diagram:
|
176 |
+
```mermaid
|
177 |
+
classDiagram
|
178 |
+
class User {
|
179 |
+
+int userId
|
180 |
+
+String name
|
181 |
+
+String email
|
182 |
+
+String password
|
183 |
+
+login()
|
184 |
+
+logout()
|
185 |
+
}
|
186 |
+
class Product {
|
187 |
+
+int productId
|
188 |
+
+String name
|
189 |
+
+String description
|
190 |
+
+float price
|
191 |
+
+int stock
|
192 |
+
+addProduct()
|
193 |
+
+removeProduct()
|
194 |
+
}
|
195 |
+
class Order {
|
196 |
+
+int orderId
|
197 |
+
+Date orderDate
|
198 |
+
+addProduct(Product product)
|
199 |
+
+removeProduct(Product product)
|
200 |
+
+calculateTotal()
|
201 |
+
}
|
202 |
+
class Payment {
|
203 |
+
+int paymentId
|
204 |
+
+float amount
|
205 |
+
+Date paymentDate
|
206 |
+
+processPayment()
|
207 |
+
}
|
208 |
+
|
209 |
+
User "1" --> "0..*" Order : places
|
210 |
+
Order "1" --> "1" Payment : has
|
211 |
+
Order "1" --> "0..*" Product : contains
|
212 |
+
Product "0..*" --> "1" Order : ordered in
|
213 |
+
```
|
214 |
+
This class diagram depicts a simplified model for an e-commerce system. Let's break down each component:
|
215 |
+
|
216 |
+
1. User Class:
|
217 |
+
- Attributes: userId, name, email, password
|
218 |
+
- Methods: login(), logout()
|
219 |
+
- Relationship: A User can place 0 to * Orders (1 to * relationship).
|
220 |
+
2. Product Class:
|
221 |
+
- Attributes: productId, name, description, price, stock
|
222 |
+
- Methods: addProduct(), removeProduct()
|
223 |
+
- Relationship: A Product can be contained in 0 to * Orders (0..* to 1 relationship).
|
224 |
+
|
225 |
+
3. Order Class:
|
226 |
+
|
227 |
+
- Attributes: orderId, orderDate
|
228 |
+
- Methods: addProduct(Product product), removeProduct(Product product), calculateTotal()
|
229 |
+
- Relationship: An Order has exactly 1 Payment (1 to 1 relationship) and can contain 0 to * Products (1 to 0..* relationship).
|
230 |
+
|
231 |
+
4. Payment Class:
|
232 |
+
- Attributes: paymentId, amount, paymentDate
|
233 |
+
- Methods: processPayment()
|
234 |
+
- Relationship: A Payment is associated with exactly 1 Order (1 to 1 relationship).
|
235 |
+
"""},
|
236 |
+
|
237 |
+
{"input": "Explain the behavior of a traffic light system.",
|
238 |
+
"output": """
|
239 |
+
To model the behavior and state transitions of a traffic light system, a state diagram is most suitable. It helps visualize the different states and the transitions between them.
|
240 |
+
|
241 |
+
State Diagram:
|
242 |
+
```mermaid
|
243 |
+
stateDiagram-v2
|
244 |
+
[*] --> Red
|
245 |
+
Red --> Green : Timer expires
|
246 |
+
Green --> Yellow : Timer expires
|
247 |
+
Yellow --> Red : Timer expires
|
248 |
+
```
|
249 |
+
"""},
|
250 |
+
{"input": "Plan a software development project.",
|
251 |
+
"output":"""
|
252 |
+
To plan a software development project, a Gantt chart is most appropriate. It provides a visual timeline of tasks, their durations, and dependencies, helping in project management.
|
253 |
+
Gant Chart:
|
254 |
+
```mermaid
|
255 |
+
gantt
|
256 |
+
title Software Development Project Plan
|
257 |
+
dateFormat YYYY-MM-DD
|
258 |
+
section Planning
|
259 |
+
Requirement Analysis :a1, 2024-07-01, 10d
|
260 |
+
Feasibility Study :a2, after a1, 7d
|
261 |
+
Project Planning :a3, after a2, 5d
|
262 |
+
section Design
|
263 |
+
System Design :b1, 2024-07-20, 15d
|
264 |
+
Architecture Design :b2, after b1, 10d
|
265 |
+
Database Design :b3, after b1, 10d
|
266 |
+
section Development
|
267 |
+
Frontend Development :c1, 2024-08-10, 20d
|
268 |
+
Backend Development :c2, after c1, 25d
|
269 |
+
API Development :c3, after c1, 15d
|
270 |
+
section Testing
|
271 |
+
Unit Testing :d1, 2024-09-10, 10d
|
272 |
+
Integration Testing :d2, after d1, 10d
|
273 |
+
System Testing :d3, after d2, 10d
|
274 |
+
section Deployment
|
275 |
+
Deployment Preparation :e1, 2024-09-30, 5d
|
276 |
+
Final Deployment :e2, after e1, 2d
|
277 |
+
```
|
278 |
+
This Gantt chart outlines a software development project plan, with tasks divided into five sections: Planning, Design, Development, Testing, and Deployment. Each section contains tasks with specified start dates and durations. Dependencies between tasks are also indicated, meaning that some tasks can only start after others are completed. Here's a breakdown:
|
279 |
+
|
280 |
+
1. Planning
|
281 |
+
- Requirement Analysis: Starts on 2024-07-01 and lasts for 10 days.
|
282 |
+
- Feasibility Study: Starts after Requirement Analysis and lasts for 7 days.
|
283 |
+
- Project Planning: Starts after Feasibility Study and lasts for 5 days.
|
284 |
+
|
285 |
+
2. Design
|
286 |
+
- System Design: Starts on 2024-07-20 and lasts for 15 days.
|
287 |
+
- Architecture Design: Starts after System Design and lasts for 10 days.
|
288 |
+
- Database Design: Starts after System Design and lasts for 10 days.
|
289 |
+
|
290 |
+
3. Development
|
291 |
+
- Frontend Development: Starts on 2024-08-10 and lasts for 20 days.
|
292 |
+
- Backend Development: Starts after Frontend Development and lasts for 25 days.
|
293 |
+
- API Development: Starts after Frontend Development and lasts for 15 days.
|
294 |
+
|
295 |
+
4. Testing
|
296 |
+
- Unit Testing: Starts on 2024-09-10 and lasts for 10 days.
|
297 |
+
- Integration Testing: Starts after Unit Testing and lasts for 10 days.
|
298 |
+
- System Testing: Starts after Integration Testing and lasts for 10 days.
|
299 |
+
|
300 |
+
5. Deployment
|
301 |
+
- Deployment Preparation: Starts on 2024-09-30 and lasts for 5 days.
|
302 |
+
- Final Deployment: Starts after Deployment Preparation and lasts for 2 days.
|
303 |
+
"""},
|
304 |
+
|
305 |
+
{"input": "Model a library system",
|
306 |
+
"output": """
|
307 |
+
To model a library system, an Entity-Relationship (ER) diagram is most appropriate. It helps visualize the entities, their attributes, and relationships.
|
308 |
+
|
309 |
+
```mermaid
|
310 |
+
erDiagram
|
311 |
+
BOOK {
|
312 |
+
int BookID PK
|
313 |
+
string Title
|
314 |
+
string Author
|
315 |
+
string ISBN
|
316 |
+
string Publisher
|
317 |
+
int YearPublished
|
318 |
+
string Genre
|
319 |
+
int CopiesAvailable
|
320 |
+
}
|
321 |
+
|
322 |
+
MEMBER {
|
323 |
+
int MemberID PK
|
324 |
+
string FirstName
|
325 |
+
string LastName
|
326 |
+
string Address
|
327 |
+
string PhoneNumber
|
328 |
+
string Email
|
329 |
+
date MembershipDate
|
330 |
+
}
|
331 |
+
|
332 |
+
LOAN {
|
333 |
+
int LoanID PK
|
334 |
+
int BookID FK
|
335 |
+
int MemberID FK
|
336 |
+
date LoanDate
|
337 |
+
date DueDate
|
338 |
+
date ReturnDate
|
339 |
+
}
|
340 |
+
|
341 |
+
BOOK ||--o{ LOAN : has
|
342 |
+
MEMBER ||--o{ LOAN : borrows
|
343 |
+
```
|
344 |
+
Entities and Attributes:
|
345 |
+
1. Book
|
346 |
+
- BookID (Primary Key)
|
347 |
+
- Title
|
348 |
+
- Author
|
349 |
+
- ISBN
|
350 |
+
- Publisher
|
351 |
+
- YearPublished
|
352 |
+
- Genre
|
353 |
+
- CopiesAvailable
|
354 |
+
|
355 |
+
2. Member
|
356 |
+
- MemberID (Primary Key)
|
357 |
+
- FirstName
|
358 |
+
- LastName
|
359 |
+
- Address
|
360 |
+
- PhoneNumber
|
361 |
+
- Email
|
362 |
+
- MembershipDate
|
363 |
+
|
364 |
+
3. Loan
|
365 |
+
- LoanID (Primary Key)
|
366 |
+
- BookID (Foreign Key)
|
367 |
+
- MemberID (Foreign Key)
|
368 |
+
- LoanDate
|
369 |
+
- DueDate
|
370 |
+
- ReturnDate
|
371 |
+
|
372 |
+
Relationships:
|
373 |
+
1. Book - Loan
|
374 |
+
- One Book can be associated with many Loans.
|
375 |
+
- One Loan is associated with one Book.
|
376 |
+
- Relationship: "Book" 1..* - 0..1 "Loan"
|
377 |
+
|
378 |
+
2. Member - Loan
|
379 |
+
- One Member can have many Loans.
|
380 |
+
- One Loan is associated with one Member.
|
381 |
+
- Relationship: "Member" 1..* - 0..1 "Loan"
|
382 |
+
"""}
|
383 |
+
]
|
main.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate
|
6 |
+
from langchain_groq import ChatGroq
|
7 |
+
from langchain_core.output_parsers import StrOutputParser
|
8 |
+
from examples import examples
|
9 |
+
from system_message import sys_msg
|
10 |
+
|
11 |
+
# Load environment variables from .env file
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
# Get the Groq API key from environment variables
|
15 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
16 |
+
|
17 |
+
if not GROQ_API_KEY:
|
18 |
+
raise ValueError("GROQ_API_KEY environment variable not set")
|
19 |
+
|
20 |
+
# FastAPI app initialization
|
21 |
+
app = FastAPI()
|
22 |
+
|
23 |
+
# Define the input data model
|
24 |
+
class PromptRequest(BaseModel):
|
25 |
+
input: str
|
26 |
+
model: str = "llama" # Default model is LLaMA
|
27 |
+
|
28 |
+
def get_final_prompt():
|
29 |
+
example_prompt = ChatPromptTemplate.from_messages(
|
30 |
+
[
|
31 |
+
("human", "{input}"),
|
32 |
+
("ai", "{output}")
|
33 |
+
]
|
34 |
+
)
|
35 |
+
|
36 |
+
few_shot_prompt = FewShotChatMessagePromptTemplate(
|
37 |
+
example_prompt=example_prompt,
|
38 |
+
examples=examples
|
39 |
+
)
|
40 |
+
|
41 |
+
# Final prompt template
|
42 |
+
final_prompt = ChatPromptTemplate.from_messages(
|
43 |
+
[
|
44 |
+
("system", sys_msg),
|
45 |
+
few_shot_prompt,
|
46 |
+
("human", "{input}")
|
47 |
+
]
|
48 |
+
)
|
49 |
+
|
50 |
+
return final_prompt
|
51 |
+
|
52 |
+
def get_model_response(model , input_text):
|
53 |
+
models = {
|
54 |
+
"mixtral": "mixtral-8x7b-32768",
|
55 |
+
"llama": "llama3-70b-8192"
|
56 |
+
}
|
57 |
+
|
58 |
+
final_prompt = get_final_prompt()
|
59 |
+
chat = ChatGroq(groq_api_key=GROQ_API_KEY, model_name=models[model], temperature=0.2)
|
60 |
+
output_parser = StrOutputParser()
|
61 |
+
chain = final_prompt | chat | output_parser
|
62 |
+
return chain.invoke({"input": input_text})
|
63 |
+
|
64 |
+
|
65 |
+
@app.post("/generate-response/")
|
66 |
+
async def generate_response(request: PromptRequest):
|
67 |
+
input_text = request.input
|
68 |
+
model_choice = request.model.lower()
|
69 |
+
|
70 |
+
response = get_model_response(model_choice , input_text)
|
71 |
+
return {"response": response}
|
72 |
+
|
73 |
+
# To run the FastAPI app, use:
|
74 |
+
# uvicorn main:app --reload
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.111.0
|
2 |
+
pydantic==2.5.3
|
3 |
+
python-dotenv==1.0.1
|
4 |
+
uvicorn==0.30.1
|
5 |
+
langchain==0.2.1
|
6 |
+
langchain-core==0.2.3
|
7 |
+
langchain-groq==0.1.4
|
system_message.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
sys_msg = """
|
2 |
+
|
3 |
+
You are an expert in generating Mermaid diagrams from natural language descriptions. Mermaid is a Markdown-inspired tool that allows users to create diagrams and visualizations using a simple syntax.
|
4 |
+
The user will ask you to explain some concept and your task is to explain concepts with the help of visualization, you can generate diagrams only if you were specifically asked to do that, but if you weren't asked to just generate a diagram and were asked to explain something then do your best to use visualization to make the explanation a lot more clear. For each input, ensure the output is a correctly formatted Mermaid diagram code that accurately represents the described structure, even if the user does not explicitly request a diagram.
|
5 |
+
|
6 |
+
Follow these guidelines for generating the correct types of diagrams:
|
7 |
+
1. Flowcharts are suitable for: Algorithm visualization, process flows ,conditional logic, and control structures (if-else, loops).
|
8 |
+
2. Sequence Diagrams are suitable for: Interaction between objects or components, communication protocols, method calls in OOP, and client-server interactions.
|
9 |
+
3. Class Diagrams are suitable for: Object-Oriented Programming (OOP) concepts, class structures, inheritance and relationships, and system architecture.
|
10 |
+
4. State Diagrams are suitable for: State machines, lifecycle of objects, protocol states, and workflow states
|
11 |
+
5. Entity-Relationship Diagrams (ERD) are suitable for: Database schema design, relationships between data entities, and data modeling.
|
12 |
+
6. Gantt Charts are suitable for: Project management, task scheduling, and timeline of events.
|
13 |
+
7. Mind Maps are suitable for: Brainstorming ideas, roadmaps, concept mapping, and hierarchical information.
|
14 |
+
|
15 |
+
When generating diagrams using the Mermaid diagramming language, ensure to follow these guidelines to avoid common syntax errors:
|
16 |
+
1. In flowcharts, if any node label contains parentheses or brackets or unicode characters, then make sure to enclose that label in double quotation marks. For example: D[replace arr[i] with arr[i+1]] will cause a syntax error while D["replace arr[i] with arr[i+1]"] won't, Also E[fact(3)] will cause a syntax error while E["fact(3)"] won't.
|
17 |
+
|
18 |
+
2. In flowcharts, to add text above arrows follow one of these formats:
|
19 |
+
- A-->|text|B (correct)
|
20 |
+
- A-- text -->B (correct)
|
21 |
+
any other format will cause a syntax error, for example this is incorrect and will cause syntax error:
|
22 |
+
- A --> text>B. (incorrect)
|
23 |
+
|
24 |
+
3. In sequence diagrams, to add notes follow this notation:
|
25 |
+
- Note [ right of | left of | over ] [Actor]: "Text in note content". (correct)
|
26 |
+
- Example:
|
27 |
+
```mermaid
|
28 |
+
sequenceDiagram
|
29 |
+
participant John
|
30 |
+
Note right of John: Text in note
|
31 |
+
``
|
32 |
+
One common format this will cause a synatx error, for example this is incorrect and will cause syntax error:
|
33 |
+
- note "Text in note content" (incorrect)
|
34 |
+
- Example:
|
35 |
+
```mermaid
|
36 |
+
sequenceDiagram
|
37 |
+
participant SQL Query
|
38 |
+
note "SQL Query is executed"
|
39 |
+
```
|
40 |
+
When responding, follow this format:
|
41 |
+
|
42 |
+
[Explanation in simple terms]
|
43 |
+
|
44 |
+
[Mermaid diagram type]:
|
45 |
+
[Your generated Mermaid code]
|
46 |
+
[Brief explanation of the Mermaid diagram]
|
47 |
+
|
48 |
+
Be precise and maintain the syntax of Mermaid diagrams. If any information is unclear or incomplete, make reasonable assumptions to complete the diagram.
|
49 |
+
"""
|