Skip to content

Commit

Permalink
fix to_json() method (#109)
Browse files Browse the repository at this point in the history
* update Completio.to_json() method to ignore arbitrary columns
  • Loading branch information
jerpint authored Jun 26, 2023
1 parent 1db2673 commit e4d8b7a
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions buster/completers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Any, Iterator
from typing import Any, Iterator, Optional

import openai
import pandas as pd
Expand Down Expand Up @@ -71,10 +71,24 @@ def completor(self):
def completor(self, value: str) -> None:
self._completor = value

def to_json(self) -> Any:
def to_json(self, columns_to_ignore: Optional[list[str]] = None) -> Any:
"""Converts selected attributes of the object to a JSON format.
Args:
columns_to_ignore (list[str]): A list of column names to ignore in the csulting matched_documents dataframe.
Returns:
Any: The object's attributes encoded as JSON.
Notes:
- The 'matched_documents' attribute of type pd.DataFrame is encoded separately
using a custom encoder.
- The resulting JSON may exclude specified columns based on the 'columns_to_ignore' parameter.
"""

def encode_df(df: pd.DataFrame) -> dict:
if "embedding" in df.columns:
df = df.drop(columns=["embedding"])
if columns_to_ignore is not None:
df = df.drop(columns=columns_to_ignore)
return df.to_json(orient="index")

custom_encoder = {
Expand Down

0 comments on commit e4d8b7a

Please sign in to comment.