From 1b71a178c02b4f8ea2c34cc773c57610b21128db Mon Sep 17 00:00:00 2001 From: cz171 Date: Fri, 17 May 2024 00:30:42 -0700 Subject: [PATCH 1/7] add app.py --- hello/{apps.py => app.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename hello/{apps.py => app.py} (100%) diff --git a/hello/apps.py b/hello/app.py similarity index 100% rename from hello/apps.py rename to hello/app.py From ec44fac26787356f953d001e38a7a8f6faa52dc1 Mon Sep 17 00:00:00 2001 From: cz171 Date: Mon, 27 May 2024 17:23:28 -0700 Subject: [PATCH 2/7] change --- Procfile | 2 +- hello/app.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/Procfile b/Procfile index a760312fc..e95726e25 100644 --- a/Procfile +++ b/Procfile @@ -1,4 +1,4 @@ -web: gunicorn gettingstarted.wsgi +web: gunicorn hello.app # Uncomment this `release` process if you are using a database, so that Django's model # migrations are run as part of app deployment, using Heroku's Release Phase feature: diff --git a/hello/app.py b/hello/app.py index 6b541fb46..464c6292e 100644 --- a/hello/app.py +++ b/hello/app.py @@ -1,6 +1,93 @@ -from django.apps import AppConfig +import singlestoredb as s2 +import json +from openai import OpenAI +import os +from flask import Flask, request, jsonify +from flask_cors import CORS # Import CORS +import logging +logging.basicConfig(level=logging.DEBUG) +app = Flask(__name__) +CORS(app) # Enable CORS for all routes +# CORS(app, resources={r"/*": {"origins": "*"}}) # This allows all domains for all routes -class HelloConfig(AppConfig): - default_auto_field = "django.db.models.BigAutoField" - name = "hello" + +conn = s2.connect('admin:niCAWET9oSGrjZjJ2X8x345dQFeQhT1t@svc-d79973dc-78a0-42df-8a25-5ab0cd3c2bc9-dml.aws-virginia-6.svc.singlestore.com:3306/llm_dataset') +os.environ['OPENAI_API_KEY']= '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' + +client = OpenAI() + +def get_embedding(text, model="text-embedding-ada-002"): + text = text.replace("\n", " ") + return client.embeddings.create(input = [text], model=model).data[0].embedding + +def get_answer(question: str): + with conn.cursor() as cur: + query_emb = get_embedding(question) + query = "SELECT text, dot_product(vector, JSON_ARRAY_PACK(%s)) as score FROM section718 where score > 0.5 order by score desc limit 5" + cur.execute(query, (json.dumps(query_emb))) + results = cur.fetchall() + # print(f"result: {results}") + sorted(results, key=lambda x: x[1], reverse=True) + texts = ['"""{}"""'.format(result[0]) for result in results] # Extracting text and surrounding it with triple quotes + # sort reference with score + + # reference = ['\n{}\n'.format(result[0]) for result in results] + texts_joined = '\n'.join(texts) # Joining all texts with a newline character for separation + # print(f"texts_joined: {texts_joined}") + completion = client.chat.completions.create( + model="ft:gpt-3.5-turbo-1106:personal::8hAW249u", + # model="gpt-3.5-turbo-1106", + messages=[ + {"role": "system", "content": "Use the provided articles delimited by newline to answer questions. If the answer cannot be found in the articles, write I could not find an answer."}, + {"role": "user", "content": f"{texts_joined}\nQuestion:{question}"}, + ] + ) + reference = "\n".join([ref[0] for ref in results[:3]]) + print(f"reference: {reference}") + answer = f"{completion.choices[0].message.content}\nReference:\n{reference}" # Not yet completing reference part + return answer + +# @app.route('/call-chatbot', methods=['POST']) +# def handle_request(): +# try: +# # Extract data from request +# data = request.json + +# # Call your codelab code with the data +# response_data = your_codelab_function(data) + +# # Return the response in JSON format +# return jsonify(response_data) +# except Exception as e: +# logging.error(f"An error occurred: {e}") +# return jsonify({"error": "An error occurred"}), 500 +@app.route('/', methods=['GET']) +def handle_request(): + try: + # Extract data from request + data = request.json + + # Call your codelab code with the data + response_data = "hello world 111" + + # Return the response in JSON format + return jsonify(response_data) + except Exception as e: + logging.error(f"An error occurred: {e}") + return jsonify({"error": "An error occurred"}), 500 + + +def your_codelab_function(data): + # Here you will process the data + # and return the response + # For example: + + question = data["question"] + answer = get_answer(question) + + # print("helloworld") + return {"response": answer} + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=8000, debug=True) \ No newline at end of file From e15a17516b59d1ae930d08d25168b6650be8e22c Mon Sep 17 00:00:00 2001 From: cz171 Date: Mon, 27 May 2024 17:29:47 -0700 Subject: [PATCH 3/7] Added dfeature From 09a8c248c34787f14353dede4e8463635bdfe08c Mon Sep 17 00:00:00 2001 From: cz171 Date: Mon, 27 May 2024 17:30:31 -0700 Subject: [PATCH 4/7] gogogo From 64cf10553be54e236d850f3c658258ceffe8752e Mon Sep 17 00:00:00 2001 From: cz171 Date: Fri, 17 May 2024 01:06:42 -0700 Subject: [PATCH 5/7] change endpoint --- hello/app.py | 11 ++++++ hello/templates/index.html | 74 ++++++++++---------------------------- 2 files changed, 29 insertions(+), 56 deletions(-) diff --git a/hello/app.py b/hello/app.py index 464c6292e..0af2db0cd 100644 --- a/hello/app.py +++ b/hello/app.py @@ -48,6 +48,7 @@ def get_answer(question: str): answer = f"{completion.choices[0].message.content}\nReference:\n{reference}" # Not yet completing reference part return answer +<<<<<<< HEAD # @app.route('/call-chatbot', methods=['POST']) # def handle_request(): # try: @@ -63,13 +64,20 @@ def get_answer(question: str): # logging.error(f"An error occurred: {e}") # return jsonify({"error": "An error occurred"}), 500 @app.route('/', methods=['GET']) +======= +@app.route('/call-chatbot', methods=['POST']) +>>>>>>> e11c7f2 (change endpoint) def handle_request(): try: # Extract data from request data = request.json # Call your codelab code with the data +<<<<<<< HEAD response_data = "hello world 111" +======= + response_data = your_codelab_function(data) +>>>>>>> e11c7f2 (change endpoint) # Return the response in JSON format return jsonify(response_data) @@ -77,7 +85,10 @@ def handle_request(): logging.error(f"An error occurred: {e}") return jsonify({"error": "An error occurred"}), 500 +<<<<<<< HEAD +======= +>>>>>>> e11c7f2 (change endpoint) def your_codelab_function(data): # Here you will process the data # and return the response diff --git a/hello/templates/index.html b/hello/templates/index.html index 55951ccc2..6287590ec 100644 --- a/hello/templates/index.html +++ b/hello/templates/index.html @@ -1,58 +1,20 @@ -{% extends "base.html" %} -{% load static %} - -{% block content %} - -
-
- -

Getting Started with Python on Heroku

-

This is a sample Python application deployed to Heroku. It's a reasonably simple app - but a good foundation for understanding how to get the most out of the Heroku platform.

- Getting Started with Python - Source on GitHub -
-
-
- -
-
-
-

How this sample app works

-
    -
  • This app was deployed to Heroku, either using Git or by using Heroku Button on the repository.
  • - -
  • When Heroku received the source code, it fetched all the dependencies in requirements.txt, creating a deployable slug.
  • -
  • The platform then spins up a dyno, a lightweight container that provides an isolated environment in which the slug can be mounted and executed.
  • -
  • You can scale your app, manage it, and deploy over 150 add-on services, from the Dashboard or CLI.
  • -
-
-
-

Next Steps

-
    -
  • If you are following the Getting Started guide, then please head back to the tutorial and follow the next steps!
  • -
  • If you deployed this app by deploying the Heroku Button, then in a command line shell, run:
  • -
      -
    • git clone https://github.com/heroku/python-getting-started.git - this will create a local copy of the source code for the app
    • -
    • cd python-getting-started - change directory into the local source code repository
    • -
    • heroku git:remote -a <your-app-name> - associate the Heroku app with the repository
    • -
    • You'll now be set up to run the app locally, or deploy changes to Heroku
    • -
    -
-

Helpful Links

- + + + + + +Chatbot UI + + + +
+
+
-
- -
-{% endblock %} + + + + \ No newline at end of file From efac0346066fa203f677b8c66339cb7e58b602a9 Mon Sep 17 00:00:00 2001 From: cz171 Date: Sat, 18 May 2024 19:41:11 -0700 Subject: [PATCH 6/7] Let's go From 3014da7de494b468f725ccfb3eda678c2dd5a544 Mon Sep 17 00:00:00 2001 From: cz171 Date: Mon, 27 May 2024 17:45:31 -0700 Subject: [PATCH 7/7] aaa --- hello/app.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/hello/app.py b/hello/app.py index 0af2db0cd..f2a7c6d36 100644 --- a/hello/app.py +++ b/hello/app.py @@ -48,7 +48,6 @@ def get_answer(question: str): answer = f"{completion.choices[0].message.content}\nReference:\n{reference}" # Not yet completing reference part return answer -<<<<<<< HEAD # @app.route('/call-chatbot', methods=['POST']) # def handle_request(): # try: @@ -64,20 +63,13 @@ def get_answer(question: str): # logging.error(f"An error occurred: {e}") # return jsonify({"error": "An error occurred"}), 500 @app.route('/', methods=['GET']) -======= -@app.route('/call-chatbot', methods=['POST']) ->>>>>>> e11c7f2 (change endpoint) def handle_request(): try: # Extract data from request data = request.json # Call your codelab code with the data -<<<<<<< HEAD response_data = "hello world 111" -======= - response_data = your_codelab_function(data) ->>>>>>> e11c7f2 (change endpoint) # Return the response in JSON format return jsonify(response_data) @@ -85,10 +77,6 @@ def handle_request(): logging.error(f"An error occurred: {e}") return jsonify({"error": "An error occurred"}), 500 -<<<<<<< HEAD - -======= ->>>>>>> e11c7f2 (change endpoint) def your_codelab_function(data): # Here you will process the data # and return the response