DSPy: The framework for programming—not prompting—language models
Python
fixing conflicting changes
Sri Vardhamanan1544082175ee
diff --git a/dsp/modules/cohere.py b/dsp/modules/cohere.pyindex e21abcbac..7fdaa547a 100644--- a/dsp/modules/cohere.py+++ b/dsp/modules/cohere.py@@ -35,7 +35,7 @@ class Cohere(LM):"""super().__init__(model)self.co = cohere.Client(api_key)-+ self.provider = "cohere"self.kwargs = {"model": model,"temperature": 0.0,diff --git a/dsp/modules/gpt3.py b/dsp/modules/gpt3.pyindex 5caeb6ed1..cc7e744e2 100644--- a/dsp/modules/gpt3.py+++ b/dsp/modules/gpt3.py@@ -29,6 +29,7 @@ class GPT3(LM):self, model: str = "text-davinci-002", api_key: Optional[str] = None, **kwargs):super().__init__(model)+ self.provider = "openai"if api_key:openai.api_key = api_key@@ -45,7 +46,7 @@ class GPT3(LM):self.history: list[dict[str, Any]] = []- def _basic_request(self, prompt: str, **kwargs) -> OpenAIObject:+ def basic_request(self, prompt: str, **kwargs) -> OpenAIObject:raw_kwargs = kwargskwargs = {**self.kwargs, **kwargs}@@ -75,7 +76,7 @@ class GPT3(LM):)def request(self, prompt: str, **kwargs) -> OpenAIObject:"""Handles retreival of GPT-3 completions whilst handling rate limiting and caching."""- return self._basic_request(prompt, **kwargs)+ return self.basic_request(prompt, **kwargs)def print_green(self, text: str, end: str = "\n"):print("\x1b[32m" + str(text) + "\x1b[0m", end=end)@@ -131,7 +132,7 @@ class GPT3(LM):Returns:list[dict[str, Any]]: list of completion choices"""-+assert only_completed, "for now"assert return_sorted is False, "for now"diff --git a/dsp/modules/hf.py b/dsp/modules/hf.pyindex b6518cdf0..e7942af2c 100644--- a/dsp/modules/hf.py+++ b/dsp/modules/hf.py@@ -26,10 +26,12 @@ def openai_to_hf(**kwargs):class HFModel(LM):def __init__(self, model, checkpoint=None, is_client=False):super().__init__(model)-+ self.provider = "hf"self.is_client = is_clientif not self.is_client:- self.model = AutoModelForSeq2SeqLM.from_pretrained(model if checkpoint is None else checkpoint).to("cuda")+ self.model = AutoModelForSeq2SeqLM.from_pretrained(+ model if checkpoint is None else checkpoint+ ).to("cuda")self.tokenizer = AutoTokenizer.from_pretrained(model)self.tokenizer.add_tokens(AddedToken("\n", normalized=False))diff --git a/dsp/modules/lm.py b/dsp/modules/lm.pyindex 0e9ff3348..2049c8355 100644--- a/dsp/modules/lm.py+++ b/dsp/modules/lm.py@@ -3,7 +3,6 @@ from abc import ABC, abstractmethodclass LM(ABC):def __init__(self, model):-self.kwargs = {"model": model,"temperature": 0.0,@@ -13,6 +12,7 @@ class LM(ABC):"presence_penalty": 0,"n": 1,}+ self.provider = "default"self.history = []@@ -30,6 +30,8 @@ class LM(ABC):print("\x1b[31m" + str(text) + "\x1b[0m", end=end)def inspect_history(self, n=1):+ provider = self.provider+last_prompt = Noneprinted = []@@ -37,7 +39,14 @@ class LM(ABC):prompt = x["prompt"]if prompt != last_prompt:- printed.append((prompt, x["response"]["choices"]))+ printed.append(+ (+ prompt,+ x["response"].generations+ if provider == "cohere"+ else x["response"]["choices"],+ )+ )last_prompt = prompt@@ -47,7 +56,9 @@ class LM(ABC):for prompt, choices in reversed(printed):print("\n\n\n")print(prompt, end="")- self.print_green(choices[0]["text"], end="")+ self.print_green(+ choices[0].text if provider == "cohere" else choices[0]["text"], end=""+ )if len(choices) > 1:self.print_red(f" \t (and {len(choices)-1} other completions)", end="")