Skip to content

Commit

Permalink
Address Ryan's comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
madiator committed Dec 14, 2024
1 parent 0b3e014 commit 8b9086f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.venv
.DS_Store
__pycache__
.vscode

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,21 @@ class Poems(BaseModel):
We define an `LLM` object that generates poems which gets applied to the topics dataset.
```python
poet = curator.LLM(
# `prompt_func` takes a row of the dataset as input.
# `row` is a dictionary with a single key 'topic' in this case.
prompt_func=lambda row: f"Write two poems about {row['topic']}.",
model_name="gpt-4o-mini",
response_format=Poems,
# `row` is the input row, and `poems` is the `Poems` class which
# is parsed from the structured output from the LLM.
parse_func=lambda row, poems: [
{"topic": row["topic"], "poem": p.poem} for p in poems.poems_list
],
)
```
Here:
* `prompt_func` takes a row of the dataset as input and returns the prompt for the LLM.
* `response_format` is the structured output class we defined above.
* `parse_func` takes the input (`row`) and the structured output (`poems`) and converts it to a list of dictionaries. This is so that we can easily convert the output to a HuggingFace Dataset object.

Now we can apply the `LLM` object to the dataset, which reads very pythonic.
```python
poem = poet(topics)
print(poem.to_pandas())
# Example output:
Expand Down
Binary file removed docs/.DS_Store
Binary file not shown.
71 changes: 36 additions & 35 deletions src/bespokelabs/curator/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,6 @@

class LLM:
"""Interface for prompting LLMs."""

@staticmethod
def _determine_backend(
model_name: str, response_format: Optional[Type[BaseModel]] = None
) -> str:
"""Determine which backend to use based on model name and response format.
Args:
model_name (str): Name of the model
response_format (Optional[Type[BaseModel]]): Response format if specified
Returns:
str: Backend to use ("openai" or "litellm")
"""
model_name = model_name.lower()

# GPT-4o models with response format should use OpenAI
if (
response_format
and OpenAIOnlineRequestProcessor(model_name).check_structured_output_support()
):
logger.info(f"Requesting structured output from {model_name}, using OpenAI backend")
return "openai"

# GPT models and O1 models without response format should use OpenAI
if not response_format and any(x in model_name for x in ["gpt-", "o1-preview", "o1-mini"]):
logger.info(f"Requesting text output from {model_name}, using OpenAI backend")
return "openai"

# Default to LiteLLM for all other cases
logger.info(
f"Requesting {f'structured' if response_format else 'text'} output from {model_name}, using LiteLLM backend"
)
return "litellm"

def __init__(
self,
model_name: str,
Expand Down Expand Up @@ -189,6 +154,42 @@ def __init__(
else:
raise ValueError(f"Unknown backend: {self.backend}")

@staticmethod
def _determine_backend(
model_name: str, response_format: Optional[Type[BaseModel]] = None
) -> str:
"""Determine which backend to use based on model name and response format.
Args:
model_name (str): Name of the model
response_format (Optional[Type[BaseModel]]): Response format if specified
Returns:
str: Backend to use ("openai" or "litellm")
"""
model_name = model_name.lower()

# GPT-4o models with response format should use OpenAI
if (
response_format
and OpenAIOnlineRequestProcessor(model_name).check_structured_output_support()
):
logger.info(f"Requesting structured output from {model_name}, using OpenAI backend")
return "openai"

# GPT models and O1 models without response format should use OpenAI
if not response_format and any(x in model_name for x in ["gpt-", "o1-preview", "o1-mini"]):
logger.info(f"Requesting text output from {model_name}, using OpenAI backend")
return "openai"

# Default to LiteLLM for all other cases
logger.info(
f"Requesting {f'structured' if response_format else 'text'} output from {model_name}, using LiteLLM backend"
)
return "litellm"



def __call__(
self,
dataset: Optional[Iterable] = None,
Expand Down

0 comments on commit 8b9086f

Please sign in to comment.