Hamid Omarov commited on
Commit
ad80689
·
1 Parent(s): 81f8e65

Day 1: Python + Flask API + LangChain prep

Browse files
Files changed (4) hide show
  1. .env +1 -0
  2. api.py +16 -18
  3. requirements.txt +3 -0
  4. test_langchain.py +26 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
api.py CHANGED
@@ -1,29 +1,27 @@
1
  from flask import Flask, request, jsonify
2
 
3
  app = Flask(__name__)
4
-
5
- # 1. /hello endpoint
6
- @app.route('/hello', methods=['GET'])
7
  def hello():
8
- return jsonify({'message': 'Hello, welcome to the RAG 30-day sprint!'})
 
9
 
10
- # 2. /calculate endpoint
11
- @app.route('/calculate', methods=['GET'])
12
  def calculate():
13
- try:
14
- a = float(request.args.get('a'))
15
- b = float(request.args.get('b'))
16
- result = a + b
17
- return jsonify({'a': a, 'b': b, 'sum': result})
18
- except (TypeError, ValueError):
19
- return jsonify({'error': 'Please provide valid numbers using ?a=5&b=10'}), 400
20
 
21
- # 3. /ai-ready endpoint
22
- @app.route('/ai-ready', methods=['GET'])
23
  def ai_ready():
24
  return jsonify({
25
- 'message': 'You are ready to build AI apps. Keep pushing, learn daily, and ship projects!'
 
26
  })
27
 
28
- if __name__ == '__main__':
29
- app.run(debug=True)
 
 
 
1
  from flask import Flask, request, jsonify
2
 
3
  app = Flask(__name__)
4
+ @app.route('/hello')
 
 
5
  def hello():
6
+ # Return a greeting message as JSON
7
+ return jsonify({"message": "Hello, World!"})
8
 
9
+ @app.route('/calculate', methods=['POST'])
 
10
  def calculate():
11
+ # Get 'a' and 'b' from request and return their sum as JSON
12
+ data = request.get_json()
13
+ a = data.get('a', 0)
14
+ b = data.get('b', 0)
15
+ return jsonify({"sum": a + b})
 
 
16
 
17
+ @app.route('/ai-ready')
 
18
  def ai_ready():
19
  return jsonify({
20
+ "message": "Day 1 of 30: Learning Python for AI",
21
+ "progress": "3%"
22
  })
23
 
24
+ # Run the Flask application
25
+ if __name__ == '__main__':
26
+ app.run(debug=True)
27
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ flask==3.0.0
2
+ requests==2.31.0
3
+ python-dotenv==1.0.0git push
test_langchain.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.llms import OpenAI
2
+ from langchain.chains import LLMChain
3
+ from langchain.prompts import PromptTemplate
4
+ import os
5
+ from dotenv import load_dotenv
6
+
7
+ # .env faylından API açarını yüklə
8
+ load_dotenv()
9
+ openai_api_key = os.getenv("OPENAI_API_KEY")
10
+
11
+ # LLM obyekti yarat (OpenAI)
12
+ llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key)
13
+
14
+ # Prompt (sual şablonu) yarat
15
+ prompt = PromptTemplate(
16
+ input_variables=["name"],
17
+ template="What would be a good company name for a company that makes {name}?"
18
+ )
19
+
20
+ # Chain yaradaraq LLM ilə promptu birləşdir
21
+ chain = LLMChain(llm=llm, prompt=prompt)
22
+
23
+ # Chain-i işə sal
24
+ response = chain.run("colorful socks")
25
+
26
+ print(response)