Skip to content

Commit

Permalink
1. Fix the issue that str result is wrapped with single quotes by not…
Browse files Browse the repository at this point in the history
…ebook.

2. Cache top_k() for better performance
  • Loading branch information
humpydonkey committed Jul 9, 2024
1 parent dac7b70 commit 6e5ac4b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 5 additions & 0 deletions vision_agent/utils/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def __init__(self, is_main_result: bool, data: Dict[str, Any]):
self.raw = copy.deepcopy(data)

self.text = data.pop(MimeType.TEXT_PLAIN, None)
if self.text and (self.text.startswith("'") and self.text.endswith("'")):
# This is a workaround for the issue that str result is wrapped with single quotes by notebook.
# E.g. input text: "'flower'". what we want: "flower"
self.text = self.text[1:-1]

self.html = data.pop(MimeType.TEXT_HTML, None)
self.markdown = data.pop(MimeType.TEXT_MARKDOWN, None)
self.svg = data.pop(MimeType.IMAGE_SVG, None)
Expand Down
8 changes: 3 additions & 5 deletions vision_agent/utils/sim.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from functools import lru_cache
from pathlib import Path
from typing import Dict, List, Optional, Sequence, Union

Expand Down Expand Up @@ -33,11 +34,7 @@ def __init__(
model: str: The model to use for embeddings.
"""
self.df = df
if not api_key:
self.client = OpenAI()
else:
self.client = OpenAI(api_key=api_key)

self.client = OpenAI(api_key=api_key)
self.model = model
if "embs" not in df.columns and sim_key is None:
raise ValueError("key is required if no column 'embs' is present.")
Expand All @@ -57,6 +54,7 @@ def save(self, sim_file: Union[str, Path]) -> None:
df = df.drop("embs", axis=1)
df.to_csv(sim_file / "df.csv", index=False)

@lru_cache(maxsize=256)
def top_k(
self, query: str, k: int = 5, thresh: Optional[float] = None
) -> Sequence[Dict]:
Expand Down

0 comments on commit 6e5ac4b

Please sign in to comment.