Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
@@ -10,64 +10,65 @@ from io import BytesIO
|
|
10 |
import chainlit as cl
|
11 |
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
name = get_image_name()
|
54 |
-
cl.user_session.set(name, image_bytes.getvalue())
|
55 |
-
cl.user_session.set("generated_image", name)
|
56 |
-
return name
|
57 |
-
|
58 |
|
59 |
-
|
60 |
-
image_name = _generate_image(prompt)
|
61 |
-
return f"Here is {image_name}."
|
62 |
|
63 |
|
64 |
-
# this is our tool - which is what allows our agent to
|
65 |
# the `description` field is of utmost imporance as it is what the LLM "brain" uses to determine
|
66 |
# which tool to use for a given input.
|
67 |
-
|
68 |
-
|
69 |
-
func=
|
70 |
-
name="
|
71 |
-
description=f"Useful
|
72 |
return_direct=True,
|
73 |
)
|
|
|
10 |
import chainlit as cl
|
11 |
|
12 |
|
13 |
+
import os
|
14 |
+
import openai
|
15 |
+
from langchain.chat_models import ChatOpenAI
|
16 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
17 |
+
from langchain.vectorstores import Chroma
|
18 |
+
from langchain.chains.question_answering import load_qa_chain
|
19 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
20 |
+
from langchain.document_loaders import UnstructuredPDFLoader
|
21 |
+
|
22 |
+
# OpenAI API Key Setup
|
23 |
+
openai.api_key = os.environ["OPENAI_API_KEY"]
|
24 |
+
|
25 |
+
# Define our RAG tool function
|
26 |
+
def rag(query):
|
27 |
+
# Load The Goal PDF
|
28 |
+
loader = UnstructuredPDFLoader("data/The Goal - A Process of Ongoing Improvement (Third Revised Edition).pdf") # , mode="elements"
|
29 |
+
docs = loader.load()
|
30 |
+
|
31 |
+
# Split Text Chunks
|
32 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
33 |
+
splits = text_splitter.split_documents(docs)
|
34 |
+
|
35 |
+
# Embed Chunks into Chroma Vector Store
|
36 |
+
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
|
37 |
+
retriever = vectorstore.as_retriever()
|
38 |
+
|
39 |
+
# Use RAG Prompt Template
|
40 |
+
prompt = hub.pull("rlm/rag-prompt")
|
41 |
+
llm = ChatOpenAI(model_name="gpt-4-1106-preview", temperature=0) # or gpt-3.5-turbo
|
42 |
+
|
43 |
+
|
44 |
+
def format_docs(docs):
|
45 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
46 |
+
|
47 |
+
|
48 |
+
rag_chain = (
|
49 |
+
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
50 |
+
| prompt
|
51 |
+
| llm
|
52 |
+
| StrOutputParser()
|
53 |
)
|
54 |
|
55 |
+
response = ""
|
56 |
+
for chunk in rag_chain.stream(query): #e.g. "What is a Bottleneck Constraint?"
|
57 |
+
cl.user_session(chunk, end="", flush=True)
|
58 |
+
response += f"\n{chunk}"
|
59 |
+
|
60 |
+
# rag_chain.invoke("What is a Bottleneck Constraint?")
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
return response
|
|
|
|
|
63 |
|
64 |
|
65 |
+
# this is our tool - which is what allows our agent to access RAG agent
|
66 |
# the `description` field is of utmost imporance as it is what the LLM "brain" uses to determine
|
67 |
# which tool to use for a given input.
|
68 |
+
rag_format = '{{"prompt": "prompt"}}'
|
69 |
+
rag_tool = Tool.from_function(
|
70 |
+
func=rag,
|
71 |
+
name="RAG",
|
72 |
+
description=f"Useful for retrieving contextual information about the PDF to answer user questions. Input should be a single string strictly in the following JSON format: {generate_image_format}",
|
73 |
return_direct=True,
|
74 |
)
|