-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from bespokelabsai/mahesh/refactor_llm
Add a SimpleLLM interface, and update documentation.
- Loading branch information
Showing
9 changed files
with
155 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.venv | ||
.DS_Store | ||
__pycache__ | ||
.vscode | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Curator example that uses `SimpleLLM` to generate poems. | ||
Please see the poem.py for more complex use cases. | ||
""" | ||
|
||
from bespokelabs import curator | ||
|
||
# Use GPT-4o-mini for this example. | ||
llm = curator.SimpleLLM(model_name="gpt-4o-mini") | ||
poem = llm("Write a poem about the importance of data in AI.") | ||
print(poem) | ||
|
||
# Use Claude 3.5 Sonnet for this example. | ||
llm = curator.SimpleLLM(model_name="claude-3-5-sonnet-20240620", backend="litellm") | ||
poem = llm("Write a poem about the importance of data in AI.") | ||
print(poem) | ||
|
||
# Note that we can also pass a list of prompts to generate multiple responses. | ||
poems = llm( | ||
[ | ||
"Write a sonnet about the importance of data in AI.", | ||
"Write a haiku about the importance of data in AI.", | ||
] | ||
) | ||
print(poems) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .dataset import Dataset | ||
from .llm.llm import LLM | ||
from .llm.simple_llm import SimpleLLM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from bespokelabs.curator.llm.llm import LLM | ||
from datasets import Dataset | ||
from typing import Union, List | ||
|
||
|
||
class SimpleLLM: | ||
"""A simpler interface for the LLM class. | ||
Usage: | ||
llm = SimpleLLM(model_name="gpt-4o-mini") | ||
llm("Do you know about the bitter lesson?") | ||
llm(["What is the capital of France?", "What is the capital of Germany?"]) | ||
For more complex use cases (e.g. structured outputs and custom prompt functions), see the LLM class. | ||
""" | ||
|
||
def __init__(self, model_name: str, backend: str = "openai"): | ||
self._model_name = model_name | ||
self._backend = backend | ||
|
||
def __call__(self, prompt: Union[str, List[str]]) -> Union[str, List[str]]: | ||
prompt_list = [prompt] if isinstance(prompt, str) else prompt | ||
dataset: Dataset = Dataset.from_dict({"prompt": prompt_list}) | ||
|
||
llm = LLM( | ||
prompt_func=lambda row: row["prompt"], | ||
model_name=self._model_name, | ||
response_format=None, | ||
backend=self._backend, | ||
) | ||
response = llm(dataset) | ||
if isinstance(prompt, str): | ||
return response["response"][0] | ||
return response["response"] |