Skip to content

Commit

Permalink
Merge pull request #5 from shxntanu/dev/shantanu
Browse files Browse the repository at this point in the history
fix breaking change
  • Loading branch information
shxntanu authored Jan 3, 2025
2 parents 8a10be8 + 3d74cb7 commit 8c71241
Show file tree
Hide file tree
Showing 5 changed files with 584 additions and 774 deletions.
1 change: 1 addition & 0 deletions lesa/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def read(

if page:
return cm.single_page_chat(file_path, page)

return cm.embed_single_document_and_chat(file_path)


Expand Down
44 changes: 26 additions & 18 deletions lesa/core/conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ def _chat(
:param context: Context for the chat
"""

STREAM: Optional[bool] = self.directory_mgr.retrieve_config_key("streaming")
STREAM: Optional[bool] = (
self.directory_mgr.retrieve_config_key("streaming") or True
)

self.console.print(
Panel(
Expand Down Expand Up @@ -125,28 +127,34 @@ def _chat(
self.console.print(
Text("Lesa: ", style="bold deep_pink1"), end=""
)
if context:
for token in chain.stream(
{"input": question, "context": context}
):
self.console.print(token, end="")
self.console.print()
else:
for token in chain.stream({"input": question}):

chain_data = {"input": question}

if context is not None:
chain_data["context"] = context

for token in chain.stream(chain_data):
# Llama3.1 returns a dictionary with 'answer' key which contains the response
# along with input and context tokens
if isinstance(token, dict):
if "answer" in token:
self.console.print(token["answer"], end="")
else:
self.console.print(token, end="")
self.console.print()
self.console.print()
time.sleep(1)

else:
# Complete Response at once (Takes longer)
with self.console.status("🧠 Thinking...") as status:
result = None
if context:
result = chain.invoke(
{"input": question, "context": context}
)
else:
result = chain.invoke({"input": question})

chain_data = {"input": question}
if context is not None:
chain_data["context"] = context

result = chain.invoke(chain_data)

status.update("🎉 Done!")
time.sleep(1)

Expand Down Expand Up @@ -216,11 +224,11 @@ def embed_single_document_and_chat(
]
)

combine_docs_chain = create_stuff_documents_chain(llm=llm, prompt=prompt)
qa_chain = create_retrieval_chain(
retriever=self.directory_mgr.vector_store.as_retriever(),
combine_docs_chain=combine_docs_chain,
combine_docs_chain=create_stuff_documents_chain(llm=llm, prompt=prompt),
)

return self._chat(qa_chain)

def start_conversation(self):
Expand Down
11 changes: 8 additions & 3 deletions lesa/core/directory_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,15 @@ def retrieve_config_key(self, key: str) -> any:
:param key: Key to retrieve
:return: Value of the key
"""
with open(self.config_file_path, "r") as f:
config_data = json.load(f)
try:
with open(self.config_file_path, "r") as f:
config_data = json.load(f)

return config_data.get(key, None)

return config_data.get(key, None)
# This error occurs when the user has not initialized the configuration
except FileNotFoundError:
return None

def update_configuration(self) -> None:
"""
Expand Down
Loading

0 comments on commit 8c71241

Please sign in to comment.