Human: You are a legendary butlazy and opportunistic data scientict/programmer.
I have this bot and i need your help in analysing itand building on it.
analyse it and put the aa flow structure in a tabuar format.
This program is 100% fuctional so consider alll the code correct. After you analsye teh code ill ask you a few questions.
Ill send you 3 files.
main.py:
# This wrapper automatically loads the API
import sys, select, time, os, subprocess, sys
if "API_SECRET" not in os.environ:
print("You must set an API_SECRET using the Secrets tool", file=sys.stderr)
elif "OPENAI_API_KEY" not in os.environ:
print("You must set an OPENAI_API_KEY using the Secrets tool", file=sys.stderr)
else:
print("== OPENAI + REPLIT CUSTOM BOT==")
print ("You have five seconds to select an option")
print()
print("1: Train Model\n2: Talk to your Bot\n3: Run API server (Default)\n> ", end="")
i, o, e = select.select( [sys.stdin], [], [], 10 )
print()
if (i):
choice = sys.stdin.readline().strip()
time.sleep(0.5)
os.system('clear')
if choice == "1":
print("BOT TRAINING MODE")
import process
process.train()
elif choice == "2":
print("BOT CONVERSATION MODE")
import process
process.runPrompt()
else:
print("Booting into API Server…")
time.sleep(1)
os.system('clear')
print("BOT API SERVER RUNNING")
p = subprocess.Popen([sys.executable, 'server.py'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
while True:
line = p.stdout.readline()
if not line: break
else:
time.sleep(0.5)
os.system('clear')
print("Booting into API Server…")
time.sleep(1)
os.system('clear')
print("BOT API SERVER RUNNING")
p = subprocess.Popen([sys.executable, 'server.py'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
while True:
line = p.stdout.readline()
if not line: break
server.py
from pathlib import Path
from langchain.text_splitter import CharacterTextSplitter
import faiss
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
import pickle
from langchain import OpenAI, LLMChain
from langchain.prompts import Prompt
from flask import Flask, request, jsonify
import os
index = faiss.read_index("training.index")
with open("faiss.pkl", "rb") as f:
store = pickle.load(f)
store.index = index
with open("training/master.txt", "r") as f:
promptTemplate = f.read()
prompt = Prompt(template=promptTemplate, input_variables=["history", "context", "question"])
app = Flask(__name__)
@app.route("/", methods=["GET"])
def index():
return "API Online"
@app.route("/", methods=["POST"])
def ask():
reqData = request.get_json()
if reqData['secret'] == os.environ["API_SECRET"]:
try:
llmChain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0, model_name="text-davinci-003", openai_api_key=os.environ["OPENAI_API_KEY"]))
def onMessage(question, history):
docs = store.similarity_search(question)
contexts = []
for i, doc in enumerate(docs):
contexts.append(f"Context {i}:\n{doc.page_content}")
answer = llmChain.predict(question=question, context="\n\n".join(contexts), history=history)
return answer
return jsonify({
"answer": onMessage(reqData['question'], reqData['history']),
"success": True
})
except:
return jsonify({
"answer": None,
"success": False,
"message": "Error"
}), 400
else:
return jsonify({
"answer": None,
"success": False,
"message": "Unauthorised"
})
app.run(host="0.0.0.0", port=3000)
process.py:
from pathlib import Path
from langchain.text_splitter import CharacterTextSplitter
import faiss
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
import pickle
from langchain import OpenAI, LLMChain
from langchain.prompts import Prompt
def train():
trainingData = list(Path("training/facts/").glob("**/*.*"))
#check there is data in the trainingData folder
if len(trainingData) < 1:
print("The folder training/facts should be populated with at least one .txt or .md file.", file=sys.stderr)
return
data = []
for training in trainingData:
with open(training) as f:
print(f"Add {f.name} to dataset")
data.append(f.read())
textSplitter = CharacterTextSplitter(chunk_size=2000, separator="\n")
docs = []
for sets in data:
docs.extend(textSplitter.split_text(sets))
store = FAISS.from_texts(docs, OpenAIEmbeddings())
faiss.write_index(store.index, "training.index")
store.index = None
with open("faiss.pkl", "wb") as f:
pickle.dump(store, f)
def runPrompt():
index = faiss.read_index("training.index")
with open("faiss.pkl", "rb") as f:
store = pickle.load(f)
store.index = index
with open("training/master.txt", "r") as f:
promptTemplate = f.read()
prompt = Prompt(template=promptTemplate, input_variables=["history", "context", "question"])
llmChain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0.25))
def onMessage(question, history):
docs = store.similarity_search(question)
contexts = []
for i, doc in enumerate(docs):
contexts.append(f"Context {i}:\n{doc.page_content}")
answer = llmChain.predict(question=question, context="\n\n".join(contexts), history=history)
return answer
history = []
while True:
question = input("Ask a question > ")
answer = onMessage(question, history)
print(f"Bot: {answer}")
history.append(f"Human: {question}")
history.append(f"Bot: {answer}")
ChatGPT:
Here is the flow structure in tabular format:
File | Function/Flow | Description |
---|---|---|
main.py | Main menu | Presents options to the user: 1. Train Model, 2. Talk to Bot, 3. Run API server |
main.py | Train Model | Imports and runs process.train() |
main.py | Talk to Bot | Imports and runs process.runPrompt() |
main.py | Run API server | Runs the server.py file |
server.py | Index route | Returns "API Online" |
server.py | Ask route | Validates secret, predicts answer based on question and history, and returns the response as JSON |
process.py | train() function | Trains the model using training data from the training/facts folder and saves the index and store as files |
process.py | runPrompt() function | Reads the index and store files, sets up the LLMChain object, and runs an interactive loop where the user can ask questions and the bot will provide answers using the model |
Human: analyse with different diagrams about entity relationshiip, sequence and state transition
ChatGPT:
Entity Relationship Diagram:
Entities:
- User
- Main Menu
- Training Model
- Bot Conversation
- API Server
- Training Data
- LLMChain
Relationships:
- User interacts with Main Menu
- Main Menu initiates Training Model
- Main Menu initiates Bot Conversation
- Main Menu initiates API Server
- Training Model processes Training Data
- Bot Conversation and API Server use LLMChain for predictions
Sequence Diagram:
- User selects an option from Main Menu 1.1. Train Model 1.1.1. Read and process Training Data 1.1.2. Train LLMChain and save index and store files 1.2. Talk to Bot 1.2.1. Load LLMChain 1.2.2. Start interactive loop 1.2.2.1. User asks a question 1.2.2.2. Bot provides an answer 1.3. Run API Server 1.3.1. Start Flask server 1.3.2. Process incoming requests 1.3.2.1. Validate secret 1.3.2.2. Generate a response using LLMChain
State Transition Diagram:
States:
- Main Menu
- Train Model
- Bot Conversation
- API Server
Transitions:
- Main Menu -> Train Model
- Main Menu -> Bot Conversation
- Main Menu -> API Server
Add a comment