DSPy: The framework for programming—not prompting—language models
Python
remove redundant code from gpt3 and abstracted class
Sri Vardhamananb621a182a752
diff --git a/dsp/modules/gpt3.py b/dsp/modules/gpt3.pyindex cc7e744e2..eefb33732 100644--- a/dsp/modules/gpt3.py+++ b/dsp/modules/gpt3.py@@ -78,38 +78,6 @@ class GPT3(LM):"""Handles retreival of GPT-3 completions whilst handling rate limiting and caching."""return self.basic_request(prompt, **kwargs)- def print_green(self, text: str, end: str = "\n"):- print("\x1b[32m" + str(text) + "\x1b[0m", end=end)-- def print_red(self, text: str, end: str = "\n"):- print("\x1b[31m" + str(text) + "\x1b[0m", end=end)-- def inspect_history(self, n: int = 1):- """Prints the last n prompts and their completions.- TODO: print the valid choice that contains filled output field instead of the first- """- last_prompt = None- printed = []-- for x in reversed(self.history[-100:]):- prompt = x["prompt"]-- if prompt != last_prompt:- printed.append((prompt, x["response"]["choices"]))-- last_prompt = prompt-- if len(printed) >= n:- break-- for prompt, choices in reversed(printed):- print("\n\n\n")- print(prompt, end="")- self.print_green(self._get_choice_text(choices[0]), end="")- if len(choices) > 1:- self.print_red(f" \t (and {len(choices)-1} other completions)", end="")- print("\n\n\n")-def _get_choice_text(self, choice: dict[str, Any]) -> str:if self.kwargs["model"] in ("gpt-3.5-turbo", "gpt-3.5-turbo-0301"):return choice["message"]["content"]diff --git a/dsp/modules/lm.py b/dsp/modules/lm.pyindex 2049c8355..9524694f3 100644--- a/dsp/modules/lm.py+++ b/dsp/modules/lm.py@@ -2,6 +2,8 @@ from abc import ABC, abstractmethodclass LM(ABC):+ """Abstract class for language models."""+def __init__(self, model):self.kwargs = {"model": model,@@ -23,14 +25,17 @@ class LM(ABC):def request(self, prompt, **kwargs):return self.basic_request(prompt, **kwargs)- def print_green(self, text, end="\n"):+ def print_green(self, text: str, end: str = "\n"):print("\x1b[32m" + str(text) + "\x1b[0m", end=end)- def print_red(self, text, end="\n"):+ def print_red(self, text: str, end: str = "\n"):print("\x1b[31m" + str(text) + "\x1b[0m", end=end)- def inspect_history(self, n=1):- provider = self.provider+ def inspect_history(self, n: int = 1):+ """Prints the last n prompts and their completions.+ TODO: print the valid choice that contains filled output field instead of the first+ """+ provider: str = self.providerlast_prompt = Noneprinted = []@@ -56,9 +61,14 @@ class LM(ABC):for prompt, choices in reversed(printed):print("\n\n\n")print(prompt, end="")- self.print_green(- choices[0].text if provider == "cohere" else choices[0]["text"], end=""- )+ text = ""+ if provider == "cohere":+ text = choices[0].text+ elif provider == "openai":+ text = self._get_choice_text(choices[0])+ else:+ text = choices[0]["text"]+ self.print_green(text, end="")if len(choices) > 1:self.print_red(f" \t (and {len(choices)-1} other completions)", end="")