Update app.py
Browse files
app.py
CHANGED
@@ -11,12 +11,9 @@ import json
|
|
11 |
|
12 |
import chainlit as cl
|
13 |
|
14 |
-
#from tiktoken import encoding_for_model
|
15 |
-
|
16 |
-
from pydantic import BaseModel, ConfigDict
|
17 |
-
|
18 |
from langchain import hub
|
19 |
from langchain_openai import OpenAI
|
|
|
20 |
from langchain.chains import LLMChain, APIChain
|
21 |
from langchain_core.prompts import PromptTemplate
|
22 |
from langchain.memory.buffer import ConversationBufferMemory
|
@@ -24,13 +21,10 @@ from langchain.memory import ConversationTokenBufferMemory
|
|
24 |
from langchain.memory import ConversationSummaryMemory
|
25 |
|
26 |
from api_docs_mck import api_docs_str
|
27 |
-
#from
|
28 |
-
#from frequently_asked_questions import instruction_text_faq, faq
|
29 |
-
|
30 |
|
31 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
32 |
|
33 |
-
# If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
34 |
daysoff_assistant_template = """
|
35 |
#You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
|
36 |
#By default, you respond in Norwegian language, using a warm, direct, and professional tone.
|
@@ -59,9 +53,7 @@ API URL:
|
|
59 |
"""
|
60 |
api_url_prompt = PromptTemplate(input_variables=['api_docs', 'question'],
|
61 |
template=api_url_template)
|
62 |
-
|
63 |
-
# If the response includes booking information, provide the information verbatim (do not summarize it.)
|
64 |
-
|
65 |
api_response_template = """
|
66 |
With the API Documentation for Daysoff's official API: {api_docs} in mind,
|
67 |
and the specific user question: {question},
|
@@ -78,19 +70,6 @@ api_response_prompt = PromptTemplate(
|
|
78 |
template=api_response_template
|
79 |
)
|
80 |
|
81 |
-
# ---------------------------------------------------------------------------------------------------------
|
82 |
-
# 100 tokens ≃ 75 words
|
83 |
-
# system prompt(s), total = 330 tokens
|
84 |
-
# average api response = 250-300 tokens (current)
|
85 |
-
# user input "reserved" = 400 tokens (300 words max. /English; Polish, Norwegian {..}?@tiktokenizer), could be reduc3d to 140 tokens ≃ 105 words
|
86 |
-
# model output (max_tokens) = 2048
|
87 |
-
|
88 |
-
# ConversationBufferMemory = maintains raw chat history; crucial for "nuanced" follow-ups (e.g. "nuanced" ~ for non-English inputs)
|
89 |
-
# ConversationTokenBufferMemory (max_token_limit) = 1318 (gives space in chat_history for approximately 10-15 exchanges, assuming ~100 tokens/exchange)
|
90 |
-
# ConversationSummaryMemory = scalable approach, especially useful for extended or complex interactions, caveat: loss of granular context
|
91 |
-
# ---------------------------------------------------------------------------------------------------------
|
92 |
-
|
93 |
-
|
94 |
@cl.on_chat_start
|
95 |
def setup_multiple_chains():
|
96 |
|
@@ -104,22 +83,10 @@ def setup_multiple_chains():
|
|
104 |
presence_penalty=0.1
|
105 |
)
|
106 |
|
107 |
-
# --ConversationBufferMemory
|
108 |
conversation_memory = ConversationBufferMemory(memory_key="chat_history",
|
109 |
-
max_len=30,
|
110 |
return_messages=True,
|
111 |
)
|
112 |
-
|
113 |
-
# --ConversationTokenBufferMemory
|
114 |
-
#conversation_memory = ConversationTokenBufferMemory(memory_key="chat_history",
|
115 |
-
#max_token_limit=1318,
|
116 |
-
#return_messages=True,
|
117 |
-
#)
|
118 |
-
|
119 |
-
# --ConversationSummaryMemory
|
120 |
-
#conversation_memory = ConversationSummaryMemory(memory_key="chat_history",
|
121 |
-
#return_messages=True,
|
122 |
-
#)
|
123 |
|
124 |
llm_chain = LLMChain(
|
125 |
llm=llm,
|
|
|
11 |
|
12 |
import chainlit as cl
|
13 |
|
|
|
|
|
|
|
|
|
14 |
from langchain import hub
|
15 |
from langchain_openai import OpenAI
|
16 |
+
from tiktoken import encoding_for_model
|
17 |
from langchain.chains import LLMChain, APIChain
|
18 |
from langchain_core.prompts import PromptTemplate
|
19 |
from langchain.memory.buffer import ConversationBufferMemory
|
|
|
21 |
from langchain.memory import ConversationSummaryMemory
|
22 |
|
23 |
from api_docs_mck import api_docs_str
|
24 |
+
#from api_docs import api_docs_str
|
|
|
|
|
25 |
|
26 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
27 |
|
|
|
28 |
daysoff_assistant_template = """
|
29 |
#You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
|
30 |
#By default, you respond in Norwegian language, using a warm, direct, and professional tone.
|
|
|
53 |
"""
|
54 |
api_url_prompt = PromptTemplate(input_variables=['api_docs', 'question'],
|
55 |
template=api_url_template)
|
56 |
+
|
|
|
|
|
57 |
api_response_template = """
|
58 |
With the API Documentation for Daysoff's official API: {api_docs} in mind,
|
59 |
and the specific user question: {question},
|
|
|
70 |
template=api_response_template
|
71 |
)
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
@cl.on_chat_start
|
74 |
def setup_multiple_chains():
|
75 |
|
|
|
83 |
presence_penalty=0.1
|
84 |
)
|
85 |
|
|
|
86 |
conversation_memory = ConversationBufferMemory(memory_key="chat_history",
|
87 |
+
max_len=30,
|
88 |
return_messages=True,
|
89 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
llm_chain = LLMChain(
|
92 |
llm=llm,
|