donb-hf commited on
Commit
764a22d
β€’
1 Parent(s): 9da5c08

update llmapp

Browse files
Files changed (7) hide show
  1. .gitignore +2 -0
  2. .python-version +1 -0
  3. __pycache__/app.cpython-311.pyc +0 -0
  4. airbnb.pdf +0 -0
  5. app.py +49 -59
  6. requirements.txt +7 -4
  7. starters.py +19 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .env
2
+ .venv/
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.11
__pycache__/app.cpython-311.pyc ADDED
Binary file (3.8 kB). View file
 
airbnb.pdf ADDED
Binary file (596 kB). View file
 
app.py CHANGED
@@ -1,79 +1,69 @@
1
- # You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
2
-
3
- # OpenAI Chat completion
4
  import os
5
- from openai import AsyncOpenAI # importing openai for API usage
6
- import chainlit as cl # importing chainlit for our app
7
- from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
8
- from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
9
  from dotenv import load_dotenv
 
 
 
 
 
 
 
 
 
10
 
11
  load_dotenv()
12
 
13
- # ChatOpenAI Templates
14
- system_template = """You are a helpful assistant who always speaks in a pleasant tone! Use your web and other tools to ensure that findings and recommendations are up to date as of June 2024.
15
- """
16
-
17
- user_template = """{input}
18
- Think through your response step by step.
19
- """
20
 
21
- @cl.on_chat_start # marks a function that will be executed at the start of a user session
22
- async def start_chat():
23
- settings = {
24
- "model": "gpt-4o",
25
- "temperature": 0,
26
- "max_tokens": 500,
27
- "top_p": 1,
28
- "frequency_penalty": 0,
29
- "presence_penalty": 0,
30
- }
31
 
32
- cl.user_session.set("settings", settings)
 
 
 
33
 
 
 
34
 
35
- @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
36
- async def main(message: cl.Message):
37
- settings = cl.user_session.get("settings")
38
 
39
- client = AsyncOpenAI()
 
 
 
 
 
 
40
 
41
- print(message.content)
42
 
43
- prompt = Prompt(
44
- provider=ChatOpenAI.id,
45
- messages=[
46
- PromptMessage(
47
- role="system",
48
- template=system_template,
49
- formatted=system_template,
50
- ),
51
- PromptMessage(
52
- role="user",
53
- template=user_template,
54
- formatted=user_template.format(input=message.content),
55
- ),
56
- ],
57
- inputs={"input": message.content},
58
- settings=settings,
59
  )
60
 
61
- print([m.to_openai() for m in prompt.messages])
 
 
 
 
62
 
63
  msg = cl.Message(content="")
64
 
65
- # Call OpenAI
66
- async for stream_resp in await client.chat.completions.create(
67
- messages=[m.to_openai() for m in prompt.messages], stream=True, **settings
68
- ):
69
- token = stream_resp.choices[0].delta.content
70
- if not token:
71
- token = ""
72
- await msg.stream_token(token)
73
 
74
- # Update the prompt object with the completion
75
- prompt.completion = msg.content
76
- msg.prompt = prompt
77
 
78
- # Send and close the message stream
79
  await msg.send()
 
 
 
 
1
  import os
2
+ import chainlit as cl
 
 
 
3
  from dotenv import load_dotenv
4
+ from operator import itemgetter
5
+ from langchain import hub
6
+ from langchain_groq import ChatGroq
7
+ from langchain_openai import OpenAIEmbeddings
8
+ from langchain_qdrant import Qdrant
9
+ from langchain_core.prompts import PromptTemplate
10
+ from langchain.schema.output_parser import StrOutputParser
11
+ from langchain.schema.runnable import RunnablePassthrough
12
+ from langchain.schema.runnable.config import RunnableConfig
13
 
14
  load_dotenv()
15
 
16
+ GROQ_API_KEY = os.environ["GROQ_API_KEY"]
17
+ OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
 
 
 
 
 
18
 
19
+ QDRANT_API_KEY = os.environ["QDRANT_API_KEY"]
20
+ QDRANT_API_URL = os.environ["QDRANT_URL"]
 
 
 
 
 
 
 
 
21
 
22
+ LANGCHAIN_PROJECT = "AirBnB PDF Jun18"
23
+ LANGCHAIN_ENDPOINT = os.environ["LANGCHAIN_ENDPOINT"]
24
+ LANGCHAIN_API_KEY = os.environ["LANGCHAIN_API_KEY"]
25
+ LANGCHAIN_TRACING_V2 = os.environ["LANGCHAIN_TRACING_V2"]
26
 
27
+ LLAMA3_PROMPT = hub.pull("rlm/rag-prompt-llama3")
28
+ # LLAMA3_PROMPT = hub.pull("cracked-nut/securities-comm-llama3-v2")
29
 
30
+ embedding = OpenAIEmbeddings(model="text-embedding-3-small")
31
+ collection = "airbnb_pdf_rec_1000_200_images"
32
+ llm = ChatGroq(model="llama3-70b-8192", temperature=0.3)
33
 
34
+ qdrant = Qdrant.from_existing_collection(
35
+ embedding=embedding,
36
+ collection_name=collection,
37
+ url=QDRANT_API_URL,
38
+ api_key=QDRANT_API_KEY,
39
+ prefer_grpc=True,
40
+ )
41
 
42
+ retriever = qdrant.as_retriever(search_kwargs={"k": 5})
43
 
44
+ @cl.on_chat_start
45
+ async def start_chat():
46
+ rag_chain = (
47
+ {"context": itemgetter("question") | retriever, "question": itemgetter("question")}
48
+ | RunnablePassthrough.assign(context=itemgetter("context"))
49
+ | {"response": LLAMA3_PROMPT | llm, "context": itemgetter("context")}
 
 
 
 
 
 
 
 
 
 
50
  )
51
 
52
+ cl.user_session.set("rag_chain", rag_chain)
53
+
54
+ @cl.on_message
55
+ async def main(message: cl.Message):
56
+ rag_chain = cl.user_session.get("rag_chain")
57
 
58
  msg = cl.Message(content="")
59
 
60
+ response = await rag_chain.ainvoke(
61
+ {"question": message.content},
62
+ config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
63
+ )
 
 
 
 
64
 
65
+ context = response["context"]
66
+ response_content = response["response"].content
 
67
 
68
+ await msg.stream_token(response_content)
69
  await msg.send()
requirements.txt CHANGED
@@ -1,5 +1,8 @@
 
 
 
 
 
1
  chainlit==0.7.700
2
- cohere==4.37
3
- openai==1.3.5
4
- tiktoken==0.5.1
5
- python-dotenv==1.0.0
 
1
+ langchain==0.2.5
2
+ langchainhub==0.1.20
3
+ langchain-openai==0.1.8
4
+ langchain-groq==0.1.5
5
+ langchain-qdrant==0.1.1
6
  chainlit==0.7.700
7
+ python-dotenv==1.0.1
8
+ openai==1.34.0
 
 
starters.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chainlit as cl
2
+
3
+ @cl.set_starters
4
+ async def set_starters():
5
+ return [
6
+ cl.Starter(
7
+ label="Description of Business",
8
+ message="What is Airbnb's 'Description of Business'?",
9
+ ),
10
+ cl.Starter(
11
+ label="Cash and cash equivalents",
12
+ message="What was the total value of 'Cash and cash equivalents' as of December 31, 2023?",
13
+ ),
14
+ cl.Starter(
15
+ label="Max shares 4Sale by Chesky",
16
+ message="What is the 'maximum number of shares to be sold under the 10b5-1 Trading plan' by Brian Chesky?",
17
+ ),
18
+ ]
19
+ # ...