camparchimedes commited on
Commit
c80f584
ยท
verified ยท
1 Parent(s): 555d347

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -19
app.py CHANGED
@@ -8,16 +8,20 @@ import time
8
  import json
9
  import torch
10
 
11
- from api_docs_mck import daysoff_api_docs_str
12
 
13
  import chainlit as cl
14
 
15
  from langchain import hub
16
  from langchain.chains import LLMChain, APIChain
17
  from langchain_core.prompts import PromptTemplate
18
- from langchain_community.llms import HuggingFaceHub
 
19
  from langchain.memory.buffer import ConversationBufferMemory
20
 
 
 
 
21
  daysoff_assistant_booking_template = """
22
  You are a customer support assistant for Daysoff.no. Your expertise is
23
  retrieving booking information for a given booking ID."
@@ -32,17 +36,17 @@ daysoff_assistant_booking_prompt= PromptTemplate(
32
 
33
  api_url_template = """
34
  Given the following API Documentation for Daysoff's official
35
- booking information API: {daysoff_api_docs}
36
  Your task is to construct the most efficient API URL to answer
37
  the user's question, ensuring the
38
  call is optimized to include only the necessary information.
39
  Question: {question}
40
  """
41
- api_url_prompt = PromptTemplate(input_variables=['daysoff_api_docs', 'question'],
42
  template=api_url_template)
43
 
44
  api_response_template = """"
45
- With the API Documentation for Daysoff's official API: {daysoff_api_docs}
46
  and the specific user question: {question} in mind,
47
  and given this API URL: {base_url} for querying, here is the
48
  response from Daysoff's API: {response}.
@@ -52,16 +56,24 @@ as if a human customer service agent is providing this information.
52
  Adapt to user's language. By default, you speak Norwegian.
53
  Booking information:
54
  """
55
- api_response_prompt = PromptTemplate(input_variables=['daysoff_api_docs',
56
- 'question',
57
  'base_url',
58
  'response'],
59
  template=api_response_template)
60
 
61
  @cl.on_chat_start
62
  def setup_multiple_chains():
63
- llm = HuggingFaceHub(repo_id="google/gemma-2-2b-it")
64
-
 
 
 
 
 
 
 
 
65
  conversation_memory = ConversationBufferMemory(memory_key="chat_history",
66
  max_len=200,
67
  return_messages=True,
@@ -75,7 +87,7 @@ def setup_multiple_chains():
75
 
76
  api_chain = APIChain.from_llm_and_api_docs(
77
  llm=llm,
78
- api_docs=daysoff_api_docs_str,
79
  api_url_prompt=api_url_prompt,
80
  api_response_prompt=api_response_prompt,
81
  verbose=True,
@@ -83,7 +95,7 @@ def setup_multiple_chains():
83
 
84
  cl.user_session.set("api_chain", api_chain)
85
 
86
- BOOKING_ID = r'\b[A-Z]{6}\d{6}\b'
87
 
88
  BOOKING_KEYWORDS = [
89
  "booking",
@@ -91,16 +103,16 @@ BOOKING_KEYWORDS = [
91
  "bookingen",
92
  "ordrenummer",
93
  "reservation",
94
- "rezerwacji",
95
- "bookingreferanse",
96
- "rezerwacja",
97
- "logg inn",
98
- "booket",
99
  "reservation number",
100
- "bestilling",
101
  "order number",
102
  "booking ID",
103
- "identyfikacyjny pล‚atnoล›ci"
104
  ]
105
 
106
  @cl.on_message
@@ -121,7 +133,7 @@ async def handle_message(message: cl.Message):
121
  else:
122
  response = await llm_chain.acall(user_message,
123
  callbacks=[cl.AsyncLangchainCallbackHandler()])
124
-
125
  response_key = "output" if "output" in response else "text"
126
  await cl.Message(response.get(response_key, "")).send()
127
  return message.content
 
8
  import json
9
  import torch
10
 
11
+ from api_docs_mck import api_docs_str
12
 
13
  import chainlit as cl
14
 
15
  from langchain import hub
16
  from langchain.chains import LLMChain, APIChain
17
  from langchain_core.prompts import PromptTemplate
18
+ #from langchain_community.llms import HuggingFaceHub
19
+ from langchain_huggingface import HuggingFaceEndpoint
20
  from langchain.memory.buffer import ConversationBufferMemory
21
 
22
+
23
+ HUGGINGFACEHUB_API_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
24
+
25
  daysoff_assistant_booking_template = """
26
  You are a customer support assistant for Daysoff.no. Your expertise is
27
  retrieving booking information for a given booking ID."
 
36
 
37
  api_url_template = """
38
  Given the following API Documentation for Daysoff's official
39
+ booking information API: {api_docs}
40
  Your task is to construct the most efficient API URL to answer
41
  the user's question, ensuring the
42
  call is optimized to include only the necessary information.
43
  Question: {question}
44
  """
45
+ api_url_prompt = PromptTemplate(input_variables=['api_docs', 'question'],
46
  template=api_url_template)
47
 
48
  api_response_template = """"
49
+ With the API Documentation for Daysoff's official API: {api_docs}
50
  and the specific user question: {question} in mind,
51
  and given this API URL: {base_url} for querying, here is the
52
  response from Daysoff's API: {response}.
 
56
  Adapt to user's language. By default, you speak Norwegian.
57
  Booking information:
58
  """
59
+ api_response_prompt = PromptTemplate(input_variables=['question',
60
+ 'api_docs',
61
  'base_url',
62
  'response'],
63
  template=api_response_template)
64
 
65
  @cl.on_chat_start
66
  def setup_multiple_chains():
67
+ #llm = HuggingFaceEndpoint(repo_id="google/gemma-2-2b-it")
68
+
69
+ llm = HuggingFaceEndpoint(
70
+ repo_id="google/gemma-2-2b-it",
71
+ huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
72
+ #max_new_tokens=512,
73
+ temperature=0.7,
74
+ task="text-generation"
75
+ )
76
+
77
  conversation_memory = ConversationBufferMemory(memory_key="chat_history",
78
  max_len=200,
79
  return_messages=True,
 
87
 
88
  api_chain = APIChain.from_llm_and_api_docs(
89
  llm=llm,
90
+ api_docs=api_docs_str,
91
  api_url_prompt=api_url_prompt,
92
  api_response_prompt=api_response_prompt,
93
  verbose=True,
 
95
 
96
  cl.user_session.set("api_chain", api_chain)
97
 
98
+ BOOKING_ID = r'\b[A-Z]{6}\d{6}\b'
99
 
100
  BOOKING_KEYWORDS = [
101
  "booking",
 
103
  "bookingen",
104
  "ordrenummer",
105
  "reservation",
106
+ "rezerwacji",
107
+ "bookingreferanse",
108
+ "rezerwacja",
109
+ "logg inn",
110
+ "booket",
111
  "reservation number",
112
+ "bestilling",
113
  "order number",
114
  "booking ID",
115
+ "identyfikacyjny pล‚atnoล›ci"
116
  ]
117
 
118
  @cl.on_message
 
133
  else:
134
  response = await llm_chain.acall(user_message,
135
  callbacks=[cl.AsyncLangchainCallbackHandler()])
136
+
137
  response_key = "output" if "output" in response else "text"
138
  await cl.Message(response.get(response_key, "")).send()
139
  return message.content