Spaces:
Runtime error
Runtime error
shaocongma
commited on
Commit
·
af971a8
1
Parent(s):
9ec586e
fix knowledge database error.
Browse filessupport OPENAI_API_BASE.
backup all generated files when possible.
- app.py +31 -59
- auto_backgrounds.py +1 -0
- kdb_test.py +62 -0
- models/embeddings.py +8 -1
- utils/file_operations.py +8 -0
- utils/knowledge.py +15 -1
app.py
CHANGED
|
@@ -1,27 +1,44 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import openai
|
| 4 |
from auto_backgrounds import generate_backgrounds, generate_draft
|
| 5 |
-
from utils.file_operations import
|
| 6 |
-
from
|
| 7 |
|
| 8 |
# todo:
|
| 9 |
# 6. get logs when the procedure is not completed. *
|
| 10 |
# 7. 自己的文件库; 更多的prompts
|
| 11 |
# 2. 实现别的功能
|
| 12 |
-
# 3. Check API Key GPT-4 Support.
|
| 13 |
# future:
|
| 14 |
# generation.log sometimes disappears (ignore this)
|
| 15 |
# 1. Check if there are any duplicated citations
|
| 16 |
# 2. Remove potential thebibliography and bibitem in .tex file
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
#######################################################################################################################
|
| 19 |
# Check if openai and cloud storage available
|
| 20 |
#######################################################################################################################
|
| 21 |
openai_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
|
| 23 |
secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
|
| 24 |
-
|
| 25 |
if access_key_id is None or secret_access_key is None:
|
| 26 |
print("Access keys are not provided. Outputs cannot be saved to AWS Cloud Storage.\n")
|
| 27 |
IS_CACHE_AVAILABLE = False
|
|
@@ -48,6 +65,13 @@ DEFAULT_SECTIONS = ["introduction", "related works", "backgrounds", "methodology
|
|
| 48 |
|
| 49 |
MODEL_LIST = ['gpt-4', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k']
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
#######################################################################################################################
|
| 52 |
# Load the list of templates & knowledge databases
|
| 53 |
#######################################################################################################################
|
|
@@ -140,7 +164,6 @@ def clear_inputs(*args):
|
|
| 140 |
def clear_inputs_refs(*args):
|
| 141 |
return "", 5
|
| 142 |
|
| 143 |
-
|
| 144 |
def wrapped_generator(
|
| 145 |
paper_title, paper_description, # main input
|
| 146 |
openai_api_key=None, openai_url=None, # key
|
|
@@ -149,10 +172,8 @@ def wrapped_generator(
|
|
| 149 |
paper_template="ICLR2022", selected_sections=None, model="gpt-4", prompts_mode=False, # outputs parameters
|
| 150 |
cache_mode=IS_CACHE_AVAILABLE # handle cache mode
|
| 151 |
):
|
| 152 |
-
# if `cache_mode` is True, then
|
| 153 |
-
|
| 154 |
-
# if so, download from the cloud storage, return it
|
| 155 |
-
# if not, generate the result.
|
| 156 |
if bib_refs is not None:
|
| 157 |
bib_refs = bib_refs.name
|
| 158 |
if openai_api_key is not None:
|
|
@@ -161,19 +182,6 @@ def wrapped_generator(
|
|
| 161 |
openai.Model.list()
|
| 162 |
except Exception as e:
|
| 163 |
raise gr.Error(f"Key错误. Error: {e}")
|
| 164 |
-
|
| 165 |
-
if cache_mode:
|
| 166 |
-
from utils.storage import list_all_files, download_file
|
| 167 |
-
# check if "title"+"description" have been generated before
|
| 168 |
-
input_dict = {"title": paper_title, "description": paper_description,
|
| 169 |
-
"generator": "generate_draft"}
|
| 170 |
-
file_name = hash_name(input_dict) + ".zip"
|
| 171 |
-
file_list = list_all_files()
|
| 172 |
-
# print(f"{file_name} will be generated. Check the file list {file_list}")
|
| 173 |
-
if file_name in file_list:
|
| 174 |
-
# download from the cloud storage, return it
|
| 175 |
-
download_file(file_name)
|
| 176 |
-
return file_name
|
| 177 |
try:
|
| 178 |
output = generate_draft(
|
| 179 |
paper_title, description=paper_description, # main input
|
|
@@ -183,19 +191,12 @@ def wrapped_generator(
|
|
| 183 |
)
|
| 184 |
if cache_mode:
|
| 185 |
from utils.storage import upload_file
|
| 186 |
-
upload_file(output)
|
| 187 |
except Exception as e:
|
| 188 |
raise gr.Error(f"生成失败. Error: {e}")
|
| 189 |
return output
|
| 190 |
|
| 191 |
|
| 192 |
-
def wrapped_references_generator(paper_title, num_refs, openai_api_key=None):
|
| 193 |
-
if openai_api_key is not None:
|
| 194 |
-
openai.api_key = openai_api_key
|
| 195 |
-
openai.Model.list()
|
| 196 |
-
return generate_top_k_references(paper_title, top_k=num_refs)
|
| 197 |
-
|
| 198 |
-
|
| 199 |
with gr.Blocks(theme=theme) as demo:
|
| 200 |
gr.Markdown(ANNOUNCEMENT)
|
| 201 |
|
|
@@ -271,9 +272,6 @@ with gr.Blocks(theme=theme) as demo:
|
|
| 271 |
max_tokens_kd_slider = gr.Slider(minimum=256, maximum=8192, value=2048, step=2,
|
| 272 |
interactive=True, label="MAX_TOKENS",
|
| 273 |
info="知识库内容占用Prompts中的Token数")
|
| 274 |
-
# template = gr.Dropdown(label="Template", choices=ALL_TEMPLATES, value="Default",
|
| 275 |
-
# interactive=True,
|
| 276 |
-
# info="生成论文的参考模板.")
|
| 277 |
domain_knowledge = gr.Dropdown(label="预载知识库",
|
| 278 |
choices=ALL_DATABASES,
|
| 279 |
value="(None)",
|
|
@@ -283,18 +281,6 @@ with gr.Blocks(theme=theme) as demo:
|
|
| 283 |
with gr.Row():
|
| 284 |
clear_button_pp = gr.Button("Clear")
|
| 285 |
submit_button_pp = gr.Button("Submit", variant="primary")
|
| 286 |
-
|
| 287 |
-
# with gr.Tab("文献搜索"):
|
| 288 |
-
# gr.Markdown(REFERENCES)
|
| 289 |
-
#
|
| 290 |
-
# title_refs = gr.Textbox(value="Playing Atari with Deep Reinforcement Learning", lines=1, max_lines=1,
|
| 291 |
-
# label="Title", info="论文标题")
|
| 292 |
-
# slider_refs = gr.Slider(minimum=1, maximum=100, value=5, step=1,
|
| 293 |
-
# interactive=True, label="最相关的参考文献数目")
|
| 294 |
-
# with gr.Row():
|
| 295 |
-
# clear_button_refs = gr.Button("Clear")
|
| 296 |
-
# submit_button_refs = gr.Button("Submit", variant="primary")
|
| 297 |
-
|
| 298 |
with gr.Tab("文献综述 (Coming soon!)"):
|
| 299 |
gr.Markdown('''
|
| 300 |
<h1 style="text-align: center;">Coming soon!</h1>
|
|
@@ -308,16 +294,6 @@ with gr.Blocks(theme=theme) as demo:
|
|
| 308 |
gr.Markdown(STATUS)
|
| 309 |
file_output = gr.File(label="Output")
|
| 310 |
json_output = gr.JSON(label="References")
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
# def wrapped_generator(
|
| 314 |
-
# paper_title, paper_description, # main input
|
| 315 |
-
# openai_api_key=None, openai_url=None, # key
|
| 316 |
-
# tldr=True, max_kw_refs=10, bib_refs=None, max_tokens_ref=2048, # references
|
| 317 |
-
# knowledge_database=None, max_tokens_kd=2048, query_counts=10, # domain knowledge
|
| 318 |
-
# paper_template="ICLR2022", selected_sections=None, model="gpt-4", prompts_mode=False, # outputs parameters
|
| 319 |
-
# cache_mode=IS_CACHE_AVAILABLE # handle cache mode
|
| 320 |
-
# ):
|
| 321 |
clear_button_pp.click(fn=clear_inputs, inputs=[title, description_pp], outputs=[title, description_pp])
|
| 322 |
submit_button_pp.click(fn=wrapped_generator,
|
| 323 |
inputs=[title, description_pp, key, url,
|
|
@@ -325,9 +301,5 @@ with gr.Blocks(theme=theme) as demo:
|
|
| 325 |
domain_knowledge, max_tokens_kd_slider, query_counts_slider,
|
| 326 |
template, sections, model_selection, prompts_mode], outputs=file_output)
|
| 327 |
|
| 328 |
-
# clear_button_refs.click(fn=clear_inputs_refs, inputs=[title_refs, slider_refs], outputs=[title_refs, slider_refs])
|
| 329 |
-
# submit_button_refs.click(fn=wrapped_references_generator,
|
| 330 |
-
# inputs=[title_refs, slider_refs, key], outputs=json_output)
|
| 331 |
-
|
| 332 |
demo.queue(concurrency_count=1, max_size=5, api_open=False)
|
| 333 |
demo.launch(show_error=True)
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import openai
|
| 5 |
from auto_backgrounds import generate_backgrounds, generate_draft
|
| 6 |
+
from utils.file_operations import list_folders, urlify
|
| 7 |
+
from huggingface_hub import snapshot_download
|
| 8 |
|
| 9 |
# todo:
|
| 10 |
# 6. get logs when the procedure is not completed. *
|
| 11 |
# 7. 自己的文件库; 更多的prompts
|
| 12 |
# 2. 实现别的功能
|
|
|
|
| 13 |
# future:
|
| 14 |
# generation.log sometimes disappears (ignore this)
|
| 15 |
# 1. Check if there are any duplicated citations
|
| 16 |
# 2. Remove potential thebibliography and bibitem in .tex file
|
| 17 |
|
| 18 |
+
#######################################################################################################################
|
| 19 |
+
# Environment Variables
|
| 20 |
+
#######################################################################################################################
|
| 21 |
+
# OPENAI_API_KEY: OpenAI API key for GPT models
|
| 22 |
+
# OPENAI_API_BASE: (Optional) Support alternative OpenAI minors
|
| 23 |
+
# GPT4_ENABLE: (Optional) Set it to 1 to enable GPT-4 model.
|
| 24 |
+
|
| 25 |
+
# AWS_ACCESS_KEY_ID: (Optional) Access AWS cloud storage (you need to edit `BUCKET_NAME` in `utils/storage.py` if you need to use this function)
|
| 26 |
+
# AWS_SECRET_ACCESS_KEY: (Optional) Access AWS cloud storage (you need to edit `BUCKET_NAME` in `utils/storage.py` if you need to use this function)
|
| 27 |
+
# KDB_REPO: (Optional) A Huggingface dataset hosting Knowledge Databases
|
| 28 |
+
# HF_TOKEN: (Optional) Access to KDB_REPO
|
| 29 |
+
|
| 30 |
#######################################################################################################################
|
| 31 |
# Check if openai and cloud storage available
|
| 32 |
#######################################################################################################################
|
| 33 |
openai_key = os.getenv("OPENAI_API_KEY")
|
| 34 |
+
openai_api_base = os.getenv("OPENAI_API_BASE")
|
| 35 |
+
if openai_api_base is not None:
|
| 36 |
+
openai.api_base = openai_api_base
|
| 37 |
+
GPT4_ENABLE = os.getenv("GPT4_ENABLE") # disable GPT-4 for public repo
|
| 38 |
+
|
| 39 |
access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
|
| 40 |
secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
|
| 41 |
+
|
| 42 |
if access_key_id is None or secret_access_key is None:
|
| 43 |
print("Access keys are not provided. Outputs cannot be saved to AWS Cloud Storage.\n")
|
| 44 |
IS_CACHE_AVAILABLE = False
|
|
|
|
| 65 |
|
| 66 |
MODEL_LIST = ['gpt-4', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k']
|
| 67 |
|
| 68 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 69 |
+
REPO_ID = os.getenv("KDB_REPO")
|
| 70 |
+
if HF_TOKEN is not None and REPO_ID is not None:
|
| 71 |
+
snapshot_download(REPO_ID, repo_type="dataset", local_dir="knowledge_databases/",
|
| 72 |
+
local_dir_use_symlinks=False, token=HF_TOKEN)
|
| 73 |
+
KDB_LIST = ["(None)"] + list_folders("knowledge_databases")
|
| 74 |
+
|
| 75 |
#######################################################################################################################
|
| 76 |
# Load the list of templates & knowledge databases
|
| 77 |
#######################################################################################################################
|
|
|
|
| 164 |
def clear_inputs_refs(*args):
|
| 165 |
return "", 5
|
| 166 |
|
|
|
|
| 167 |
def wrapped_generator(
|
| 168 |
paper_title, paper_description, # main input
|
| 169 |
openai_api_key=None, openai_url=None, # key
|
|
|
|
| 172 |
paper_template="ICLR2022", selected_sections=None, model="gpt-4", prompts_mode=False, # outputs parameters
|
| 173 |
cache_mode=IS_CACHE_AVAILABLE # handle cache mode
|
| 174 |
):
|
| 175 |
+
# if `cache_mode` is True, then always upload the generated content to my S3.
|
| 176 |
+
file_name_upload = urlify(paper_title) + "_" + uuid.uuid1().hex + ".zip"
|
|
|
|
|
|
|
| 177 |
if bib_refs is not None:
|
| 178 |
bib_refs = bib_refs.name
|
| 179 |
if openai_api_key is not None:
|
|
|
|
| 182 |
openai.Model.list()
|
| 183 |
except Exception as e:
|
| 184 |
raise gr.Error(f"Key错误. Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
try:
|
| 186 |
output = generate_draft(
|
| 187 |
paper_title, description=paper_description, # main input
|
|
|
|
| 191 |
)
|
| 192 |
if cache_mode:
|
| 193 |
from utils.storage import upload_file
|
| 194 |
+
upload_file(output, target_name=file_name_upload)
|
| 195 |
except Exception as e:
|
| 196 |
raise gr.Error(f"生成失败. Error: {e}")
|
| 197 |
return output
|
| 198 |
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
with gr.Blocks(theme=theme) as demo:
|
| 201 |
gr.Markdown(ANNOUNCEMENT)
|
| 202 |
|
|
|
|
| 272 |
max_tokens_kd_slider = gr.Slider(minimum=256, maximum=8192, value=2048, step=2,
|
| 273 |
interactive=True, label="MAX_TOKENS",
|
| 274 |
info="知识库内容占用Prompts中的Token数")
|
|
|
|
|
|
|
|
|
|
| 275 |
domain_knowledge = gr.Dropdown(label="预载知识库",
|
| 276 |
choices=ALL_DATABASES,
|
| 277 |
value="(None)",
|
|
|
|
| 281 |
with gr.Row():
|
| 282 |
clear_button_pp = gr.Button("Clear")
|
| 283 |
submit_button_pp = gr.Button("Submit", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
with gr.Tab("文献综述 (Coming soon!)"):
|
| 285 |
gr.Markdown('''
|
| 286 |
<h1 style="text-align: center;">Coming soon!</h1>
|
|
|
|
| 294 |
gr.Markdown(STATUS)
|
| 295 |
file_output = gr.File(label="Output")
|
| 296 |
json_output = gr.JSON(label="References")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
clear_button_pp.click(fn=clear_inputs, inputs=[title, description_pp], outputs=[title, description_pp])
|
| 298 |
submit_button_pp.click(fn=wrapped_generator,
|
| 299 |
inputs=[title, description_pp, key, url,
|
|
|
|
| 301 |
domain_knowledge, max_tokens_kd_slider, query_counts_slider,
|
| 302 |
template, sections, model_selection, prompts_mode], outputs=file_output)
|
| 303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
demo.queue(concurrency_count=1, max_size=5, api_open=False)
|
| 305 |
demo.launch(show_error=True)
|
auto_backgrounds.py
CHANGED
|
@@ -295,6 +295,7 @@ if __name__ == "__main__":
|
|
| 295 |
import openai
|
| 296 |
|
| 297 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 298 |
|
| 299 |
target_title = "Playing Atari with Decentralized Reinforcement Learning"
|
| 300 |
output = generate_draft(target_title, knowledge_database="ml_textbook_test")
|
|
|
|
| 295 |
import openai
|
| 296 |
|
| 297 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 298 |
+
openai.api_base = os.getenv("OPENAI_API_BASE")
|
| 299 |
|
| 300 |
target_title = "Playing Atari with Decentralized Reinforcement Learning"
|
| 301 |
output = generate_draft(target_title, knowledge_database="ml_textbook_test")
|
kdb_test.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from utils.knowledge import Knowledge
|
| 2 |
+
from langchain.vectorstores import FAISS
|
| 3 |
+
from utils.file_operations import list_folders
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import os
|
| 7 |
+
import json
|
| 8 |
+
from models import EMBEDDINGS
|
| 9 |
+
|
| 10 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
+
REPO_ID = os.getenv("KDB_REPO")
|
| 12 |
+
|
| 13 |
+
snapshot_download(REPO_ID, repo_type="dataset", local_dir="knowledge_databases/",
|
| 14 |
+
local_dir_use_symlinks=False, token=HF_TOKEN)
|
| 15 |
+
ALL_KDB = ["(None)"] + list_folders("knowledge_databases")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def query_from_kdb(input, kdb, query_counts):
|
| 20 |
+
if kdb == "(None)":
|
| 21 |
+
return {"knowledge_database": "(None)", "input": input, "output": ""}, ""
|
| 22 |
+
|
| 23 |
+
db_path = f"knowledge_databases/{kdb}"
|
| 24 |
+
db_config_path = os.path.join(db_path, "db_meta.json")
|
| 25 |
+
db_index_path = os.path.join(db_path, "faiss_index")
|
| 26 |
+
if os.path.isdir(db_path):
|
| 27 |
+
# load configuration file
|
| 28 |
+
with open(db_config_path, "r", encoding="utf-8") as f:
|
| 29 |
+
db_config = json.load(f)
|
| 30 |
+
model_name = db_config["embedding_model"]
|
| 31 |
+
embeddings = EMBEDDINGS[model_name]
|
| 32 |
+
db = FAISS.load_local(db_index_path, embeddings)
|
| 33 |
+
knowledge = Knowledge(db=db)
|
| 34 |
+
knowledge.collect_knowledge({input: query_counts}, max_query=query_counts)
|
| 35 |
+
domain_knowledge = knowledge.to_json()
|
| 36 |
+
else:
|
| 37 |
+
raise RuntimeError(f"Failed to query from FAISS.")
|
| 38 |
+
return domain_knowledge, ""
|
| 39 |
+
|
| 40 |
+
ANNOUNCEMENT = """"""
|
| 41 |
+
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.HTML(ANNOUNCEMENT)
|
| 44 |
+
with gr.Row():
|
| 45 |
+
with gr.Column():
|
| 46 |
+
kdb_dropdown = gr.Dropdown(choices=ALL_KDB, value="(None)")
|
| 47 |
+
user_input = gr.Textbox(label="Input")
|
| 48 |
+
button_retrieval = gr.Button("Query", variant="primary")
|
| 49 |
+
|
| 50 |
+
with gr.Accordion("Advanced Setting", open=False):
|
| 51 |
+
query_counts_slider = gr.Slider(minimum=1, maximum=20, value=10, step=1,
|
| 52 |
+
interactive=True, label="QUERY_COUNTS",
|
| 53 |
+
info="从知识库内检索多少条内容")
|
| 54 |
+
|
| 55 |
+
retrieval_output = gr.JSON(label="Output")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
button_retrieval.click(fn=query_from_kdb, inputs=[user_input, kdb_dropdown, query_counts_slider], outputs=[retrieval_output, user_input])
|
| 59 |
+
demo.queue(concurrency_count=1, max_size=5, api_open=False)
|
| 60 |
+
demo.launch(show_error=True)
|
| 61 |
+
|
| 62 |
+
|
models/embeddings.py
CHANGED
|
@@ -1,5 +1,12 @@
|
|
| 1 |
from langchain.embeddings import HuggingFaceEmbeddings
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
model_name = 'sentence-transformers/all-MiniLM-L6-v2'
|
| 5 |
model_kwargs = {'device': 'cpu'}
|
|
@@ -11,4 +18,4 @@ all_minilm_l6_v2 = HuggingFaceEmbeddings(
|
|
| 11 |
encode_kwargs=encode_kwargs)
|
| 12 |
|
| 13 |
|
| 14 |
-
EMBEDDINGS = {"all-MiniLM-L6-v2": all_minilm_l6_v2}
|
|
|
|
| 1 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 2 |
+
import os
|
| 3 |
|
| 4 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 5 |
+
if openai_api_key is not None:
|
| 6 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 7 |
+
openai_embedding = OpenAIEmbeddings(model="text-embedding-ada-002", openai_api_key=openai_api_key)
|
| 8 |
+
else:
|
| 9 |
+
openai_embedding = None
|
| 10 |
|
| 11 |
model_name = 'sentence-transformers/all-MiniLM-L6-v2'
|
| 12 |
model_kwargs = {'device': 'cpu'}
|
|
|
|
| 18 |
encode_kwargs=encode_kwargs)
|
| 19 |
|
| 20 |
|
| 21 |
+
EMBEDDINGS = {"text-embedding-ada-002": openai_embedding, "all-MiniLM-L6-v2": all_minilm_l6_v2}
|
utils/file_operations.py
CHANGED
|
@@ -2,6 +2,14 @@ import hashlib
|
|
| 2 |
import os, shutil
|
| 3 |
import datetime
|
| 4 |
from utils.tex_processing import replace_title
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def hash_name(input_dict):
|
| 7 |
'''
|
|
|
|
| 2 |
import os, shutil
|
| 3 |
import datetime
|
| 4 |
from utils.tex_processing import replace_title
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
def urlify(s):
|
| 8 |
+
# Remove all non-word characters (everything except numbers and letters)
|
| 9 |
+
s = re.sub(r"[^\w\s]", '', s)
|
| 10 |
+
# Replace all runs of whitespace with a single dash
|
| 11 |
+
s = re.sub(r"\s+", '_', s)
|
| 12 |
+
return s
|
| 13 |
|
| 14 |
def hash_name(input_dict):
|
| 15 |
'''
|
utils/knowledge.py
CHANGED
|
@@ -44,4 +44,18 @@ class Knowledge:
|
|
| 44 |
break
|
| 45 |
else:
|
| 46 |
prompts.append(prompt)
|
| 47 |
-
return "".join(prompts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
break
|
| 45 |
else:
|
| 46 |
prompts.append(prompt)
|
| 47 |
+
return "".join(prompts)
|
| 48 |
+
|
| 49 |
+
def to_json(self):
|
| 50 |
+
if len(self.contents) == 0:
|
| 51 |
+
return {}
|
| 52 |
+
output = {}
|
| 53 |
+
for idx, content in enumerate(self.contents):
|
| 54 |
+
output[str(idx)] = {
|
| 55 |
+
"content": content["content"],
|
| 56 |
+
"score": str(content["score"])
|
| 57 |
+
}
|
| 58 |
+
print(output)
|
| 59 |
+
return output
|
| 60 |
+
|
| 61 |
+
|