Spaces:
Sleeping
Sleeping
Hamid Omarov
commited on
Commit
·
ad80689
1
Parent(s):
81f8e65
Day 1: Python + Flask API + LangChain prep
Browse files- .env +1 -0
- api.py +16 -18
- requirements.txt +3 -0
- 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 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
@app.route('/calculate', methods=['GET'])
|
| 12 |
def calculate():
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
except (TypeError, ValueError):
|
| 19 |
-
return jsonify({'error': 'Please provide valid numbers using ?a=5&b=10'}), 400
|
| 20 |
|
| 21 |
-
|
| 22 |
-
@app.route('/ai-ready', methods=['GET'])
|
| 23 |
def ai_ready():
|
| 24 |
return jsonify({
|
| 25 |
-
|
|
|
|
| 26 |
})
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
| 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)
|