cmagganas commited on
Commit
98997fa
1 Parent(s): 870a04d

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +53 -52
tools.py CHANGED
@@ -10,64 +10,65 @@ from io import BytesIO
10
  import chainlit as cl
11
 
12
 
13
- def get_image_name():
14
- """
15
- We need to keep track of images we generate, so we can reference them later
16
- and display them correctly to our users.
17
- """
18
- image_count = cl.user_session.get("image_count")
19
- if image_count is None:
20
- image_count = 0
21
- else:
22
- image_count += 1
23
-
24
- cl.user_session.set("image_count", image_count)
25
-
26
- return f"image-{image_count}"
27
-
28
-
29
- def _generate_image(prompt: str):
30
- """
31
- This function is used to generate an image from a text prompt using
32
- DALL-E 3.
33
-
34
- We use the OpenAI API to generate the image, and then store it in our
35
- user session so we can reference it later.
36
- """
37
- client = OpenAI()
38
-
39
- response = client.images.generate(
40
- model="dall-e-3",
41
- prompt=prompt,
42
- size="1024x1024",
43
- quality="standard",
44
- n=1,
 
 
 
 
 
 
 
 
45
  )
46
 
47
- image_payload = requests.get(response.data[0].url, stream=True)
48
-
49
- image_bytes = BytesIO(image_payload.content)
50
-
51
- print(type(image_bytes))
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
- def generate_image(prompt: str):
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 generate images in the first place!
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
- generate_image_format = '{{"prompt": "prompt"}}'
68
- generate_image_tool = Tool.from_function(
69
- func=generate_image,
70
- name="GenerateImage",
71
- description=f"Useful to create an image from a text prompt. Input should be a single string strictly in the following JSON format: {generate_image_format}",
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
  )