Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New branch #50

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/workflow.yml/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
steps:
- name: My step
env:
API_KEY: ${{ secrets.APIKEY }}
run: |
# You can use the secret as an environment variable in this step
echo "API key is $API_KEY"
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#simple_server exoframe login https://exoframe.xf.mkrs.link -k /Users/fawaztarar/Documents/makers/key.pem



# Imagine this file as a recipe for setting up a virtual computer.

# Dockerfiles typically start with a 'base image'. There are loads of these
# and you can find them at hub.docker.com.
# We're going to use a base image for Python veresion 3.11
FROM python:3.11

# This base image contains essentially everything necessary for a 'virtual
# computer'. It has a terminal, certain basic commands, and of course Python.

# We run a command to install `pipenv`
RUN pip install pipenv

# We'll need our app's files in the container in order to be able to run them!
# We copy them in from the current directory to the folder `/app` in
# our virtual computer. Reminder `.` means 'the current directory'
COPY . /app

# We set the working directory for commands from this point on
WORKDIR /app

# We run `pipenv install` to install our project's dependencies. Since we've
# copied in our `Pipfile`, `pipenv` will use that to get a list of dependencies.
# We include a couple of extra options suitable for deployment.
RUN pipenv install --system --deploy

# At this point we've set up our virtual computer, but we've not _yet_ run our
# application. And we're not going to! We're just setting up the container
# so that it's ready to do so when we tell it.

# So we're going to tell Docker here that when we _do_ want to run it, this is
# what it should run:
CMD ["python", "app.py"]
11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.11"
20 changes: 20 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 43 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
# chatgpt-retrieval
## Chatbot for makers.tech

openAI chatgpt3 free tier integration
Chatbot data from data files PDF, text
chatbot data extraction from makers.tech


## update Constants
# Replace with your own OpenAI API Key https://platform.openai.com/account/api-keys
# and rename this file to constants.py.
#APIKEY = "sk-<your key here>"


Simple script to use ChatGPT on your own files.

Here's the [YouTube Video](https://youtu.be/9AXP7tCI9PI).

## Installation

Install [Langchain](https://github.com/hwchase17/langchain) and other required packages.
```
pip install langchain openai chromadb tiktoken unstructured
```
Modify `constants.py.default` to use your own [OpenAI API key](https://platform.openai.com/account/api-keys), and rename it to `constants.py`.

Place your own data into `data/data.txt`.

## Example usage
Test reading `data/data.txt` file.
```
> python chatgpt.py "what is my dog's name"
Your dog's name is Sunny.
```

Test reading `data/cat.pdf` file.
```
> python chatgpt.py "what is my cat's name"
Your cat's name is Muffy.
```
1. pipenv install

2. pinev shell

3. pip install pytest


3. pip install flask

4. pip install beautifulsoup4

pip install requests beautifulsoup4


5. pip install PyPDF2

6. pip install scrapy

7. pytest

8. pip install aiofiles

9. pip install elasticsearch

10. psql CREATE DATABASE chatbot_db;

11. pip install Flask-SQLAlchemy psycopg2-binary

12. pip install pdfplumber


139 changes: 89 additions & 50 deletions chatgpt.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,91 @@
import os
import sys

import os
import openai
from langchain.chains import ConversationalRetrievalChain, RetrievalQA
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import DirectoryLoader, TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.indexes import VectorstoreIndexCreator
from langchain.indexes.vectorstore import VectorStoreIndexWrapper
from langchain.llms import OpenAI
from langchain.vectorstores import Chroma

import constants

os.environ["OPENAI_API_KEY"] = constants.APIKEY

# Enable to save to disk & reuse the model (for repeated queries on the same data)
PERSIST = False

query = None
if len(sys.argv) > 1:
query = sys.argv[1]

if PERSIST and os.path.exists("persist"):
print("Reusing index...\n")
vectorstore = Chroma(persist_directory="persist", embedding_function=OpenAIEmbeddings())
index = VectorStoreIndexWrapper(vectorstore=vectorstore)
else:
#loader = TextLoader("data/data.txt") # Use this line if you only need data.txt
loader = DirectoryLoader("data/")
if PERSIST:
index = VectorstoreIndexCreator(vectorstore_kwargs={"persist_directory":"persist"}).from_loaders([loader])
else:
index = VectorstoreIndexCreator().from_loaders([loader])

chain = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(model="gpt-3.5-turbo"),
retriever=index.vectorstore.as_retriever(search_kwargs={"k": 1}),
)

chat_history = []
while True:
if not query:
query = input("Prompt: ")
if query in ['quit', 'q', 'exit']:
sys.exit()
result = chain({"question": query, "chat_history": chat_history})
print(result['answer'])

chat_history.append((query, result['answer']))
query = None
import requests
from flask import Flask, request, jsonify, render_template
from flask import current_app
import json
from PyPDF2 import PdfReader
from flask_sqlalchemy import SQLAlchemy
import pdfplumber
from bs4 import BeautifulSoup
from lib.config import Config
from lib.db_models import db
from lib.db_models import ExtractedData
from lib.db_models import create_app, db, store_data, query_data
from lib.search import simple_search
from flask import Flask
from lib.config import Config
from lib.db_models import db



from flask import Flask
from lib.db_models import db


def create_app():
app = Flask(__name__)
app.config.from_object(Config)

# Initialize extensions with the app instance
db.init_app(app)

return app







@app.route('/')
def chatbot():
return render_template('ai_chatbot.html')

@app.route('/query', methods=['POST'])
def handle_query():
data_request = request.json
query = data_request.get('query')

if not query:
return jsonify({"error": "No query provided"}), 400

context = simple_search(query)

try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": query},
{"role": "assistant", "content": context},
]
)
answer = response.choices[0].message['content']
except Exception as e:
return jsonify({"error": str(e)}), 500

return jsonify({"answer": answer})



@app.route('/test_error')
def test_error():
raise Exception('Test exception')


if __name__ == '__main__':
app.run(debug=False, port=5001) # Set to False in production












3 changes: 0 additions & 3 deletions constants.py.default

This file was deleted.

Binary file removed data/cat.pdf
Binary file not shown.
Loading