Skip to content

Commit

Permalink
Merge branch 'stanfordnlp:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasahle authored Mar 14, 2024
2 parents 69cfd9f + 51d2d4f commit d61ea31
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 201 deletions.
25 changes: 11 additions & 14 deletions dsp/modules/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def backoff_hdlr(details):
"""Handler from https://pypi.org/project/backoff/"""
"""Handler from https://pypi.org/project/backoff/."""
print(
"Backing off {wait:0.1f} seconds after {tries} tries "
"calling function {target} with kwargs "
Expand All @@ -28,7 +28,7 @@ def backoff_hdlr(details):


def giveup_hdlr(details):
"""wrapper function that decides when to give up on retry"""
"""Wrapper function that decides when to give up on retry."""
if "rate limits" in details.message:
return False
return True
Expand All @@ -46,19 +46,19 @@ def __init__(
super().__init__(model)

try:
from anthropic import Anthropic, RateLimitError
from anthropic import Anthropic
except ImportError as err:
raise ImportError("Claude requires `pip install anthropic`.") from err

self.provider = "anthropic"
self.api_key = api_key = os.environ.get("ANTHROPIC_API_KEY") if api_key is None else api_key
self.api_base = BASE_URL if api_base is None else api_base

self.kwargs = {
"temperature": 0.0 if "temperature" not in kwargs else kwargs["temperature"],
"temperature": kwargs.get("temperature", 0.0),
"max_tokens": min(kwargs.get("max_tokens", 4096), 4096),
"top_p": 1.0 if "top_p" not in kwargs else kwargs["top_p"],
"top_k": 1 if "top_k" not in kwargs else kwargs["top_k"],
"top_p": kwargs.get("top_p", 1.0),
"top_k": kwargs.get("top_k", 1),
"n": kwargs.pop("n", kwargs.pop("num_generations", 1)),
**kwargs,
}
Expand All @@ -80,7 +80,6 @@ def basic_request(self, prompt: str, **kwargs):
# caching mechanism requires hashable kwargs
kwargs["messages"] = [{"role": "user", "content": prompt}]
kwargs.pop("n")
print(kwargs)
response = self.client.messages.create(**kwargs)

history = {
Expand All @@ -102,7 +101,7 @@ def basic_request(self, prompt: str, **kwargs):
giveup=giveup_hdlr,
)
def request(self, prompt: str, **kwargs):
"""Handles retrieval of completions from Anthropic whilst handling API errors"""
"""Handles retrieval of completions from Anthropic whilst handling API errors."""
return self.basic_request(prompt, **kwargs)

def __call__(self, prompt, only_completed=True, return_sorted=False, **kwargs):
Expand All @@ -120,21 +119,19 @@ def __call__(self, prompt, only_completed=True, return_sorted=False, **kwargs):
assert only_completed, "for now"
assert return_sorted is False, "for now"


# per eg here: https://docs.anthropic.com/claude/reference/messages-examples
# max tokens can be used as a proxy to return smaller responses
# so this cannot be a proper indicator for incomplete response unless it isnt the user-intent.
# if only_completed and response.stop_reason != "end_turn":
# choices = []

n = kwargs.pop("n", 1)
completions = []
for i in range(n):
for _ in range(n):
response = self.request(prompt, **kwargs)
# TODO: Log llm usage instead of hardcoded openai usage
# if dsp.settings.log_openai_usage:
# self.log_usage(response)
if only_completed and response.stop_reason == "max_tokens":
continue
completions = [c.text for c in response.content]
return completions
return completions
183 changes: 0 additions & 183 deletions dspy/experimental/synthesizer.py

This file was deleted.

1 change: 1 addition & 0 deletions dspy/experimental/synthesizer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .synthesizer import *
24 changes: 24 additions & 0 deletions dspy/experimental/synthesizer/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Any, Optional

from pydantic import BaseModel, model_validator


class SynthesizerArguments(BaseModel):
feedback_mode: Optional[str] = None
num_example_for_feedback: Optional[int] = None

input_lm_model: Optional[Any] = None
output_lm_model: Optional[Any] = None
output_teacher_module: Optional[Any] = None

num_example_for_optim: Optional[int] = None

@model_validator(mode='after')
def validate_feedback_mode(self):
if self.feedback_mode and self.feedback_mode not in ["human", "llm"]:
raise ValueError("Feedback mode should be either 'human' or 'llm'.")

if self.feedback_mode and not self.num_example_for_feedback:
raise ValueError("Number of examples for feedback is required when feedback mode is provided.")

return self
3 changes: 3 additions & 0 deletions dspy/experimental/synthesizer/instruction_suffixes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INPUT_GENERATION_TASK_WITH_EXAMPLES_SUFFIX = """\n\nI'll also be providing you some data I generated before hand, make sure the data you generate if consistent with task I provided but different from the data I provided in every way possible."""

INPUT_GENERATION_TASK_WITH_FEEDBACK_SUFFIX = "\n\nAdditionally, I'll be providing you with feedback on the data you generate, while generating the data make sure to take into account the feedback I provide and try to improve the data you generate based on the feedback I provide."
Loading

0 comments on commit d61ea31

Please sign in to comment.