Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Playing around with loading collection data into whinchat #1015

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions pydatalab/src/pydatalab/apps/chat/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ def plot_functions(self):
def render(self):
if not self.data.get("messages"):
if (item_id := self.data.get("item_id")) is not None:
info_json = self._prepare_item_json_for_chat(item_id)
info_json, collections = self._prepare_item_json_for_chat(item_id)
if len(collections):
info_json = self._prepare_collection_json_for_chat(
collections[0]["collection_id"]
)
elif (collection_id := self.data.get("collection_id")) is not None:
info_json = self._prepare_collection_json_for_chat(collection_id)
else:
Expand All @@ -129,7 +133,7 @@ def render(self):
},
{
"role": "user",
"content": f"""Here is the JSON data for the current item(s): {info_json}.
"content": f"""Here is the JSON data for the current item, {item_id}. If the item is in a collection, the data for the rest of the items in the collection are also given: {info_json}.
Start with a friendly introduction and give me a one sentence summary of what this is (not detailed, no information about specific masses). """,
},
]
Expand Down Expand Up @@ -315,7 +319,7 @@ def _prepare_item_json_for_chat(self, item_id: str):
.replace(r"\n", " ")
)

return item_info_json
return item_info_json, item_info.get("collections", [])

def _prepare_collection_json_for_chat(self, collection_id: str):
from pydatalab.routes.v0_1.collections import get_collection
Expand All @@ -325,8 +329,8 @@ def _prepare_collection_json_for_chat(self, collection_id: str):
raise RuntimeError(f"Attempt to get collection data for {collection_id} failed.")

children = collection_data["child_items"]
return (
"["
+ ",".join([self._prepare_item_json_for_chat(child["item_id"]) for child in children])
+ "]"
)
jsons = []
for child in children:
jsons.append(self._prepare_item_json_for_chat(child["item_id"])[0])

return "[" + ",".join(jsons) + "]"