Skip to content

Commit

Permalink
LLM docstrings + documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
parkervg committed Feb 27, 2024
1 parent a3f51c6 commit ccf5767
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 26 additions & 1 deletion blendsql/llms/_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ def __attrs_post_init__(self):
self.model = self._load_model()

def predict(self, program: Program, **kwargs) -> dict:
"""Takes a `Program` and some kwargs, and evaluates it with context of
current LLM.
Args:
program: guidance program used to generate LLM output
**kwargs: any additional kwargs will get passed to the program
Returns:
dict containing all LLM variable names and their values.
Example:
>>> {"result": '"This is LLM generated output"'}
"""
key = self._create_key(program, **kwargs)
if key in self.cache:
return self.cache.get(key)
Expand All @@ -96,7 +109,14 @@ def predict(self, program: Program, **kwargs) -> dict:
self.cache[key] = model._variables
return model._variables

def _create_key(self, program: Program, **kwargs):
def _create_key(self, program: Program, **kwargs) -> str:
"""Generates a hash to use in diskcache Cache.
This way, we don't need to send our prompts to the same LLM
if our context of LLM + program + kwargs is the same.
Returns:
md5 hash used as key in diskcache
"""
hasher = hashlib.md5()
# Ignore partials, which create a random key within session
options_str = str(
Expand All @@ -113,7 +133,12 @@ def _create_key(self, program: Program, **kwargs):
return hasher.hexdigest()

def _setup(self, **kwargs) -> None:
"""Any additional setup required to get this LLM up and functioning
should go here. For example, in the AzureOpenaiLLM, we have some logic
to refresh our client secrets every 30 min.
"""
...

def _load_model(self) -> guidance.models.Model:
"""Logic for instantiating the guidance model class goes here."""
...
2 changes: 2 additions & 0 deletions docs/reference/blenders/blenders.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ We enable integration with many existing LLMs by building on top of [`guidance`

Certain models may be better geared towards some BlendSQL tasks than others, so choose carefully!


## `LLM`
::: blendsql.llms._llm.LLM
handler: python
show_source: true

0 comments on commit ccf5767

Please sign in to comment.