Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

(feature) Adding support for Mistral AI's API

Franck Stephane Ndzomga12b86830d453
diff --git a/dsp/modules/lm.py b/dsp/modules/lm.py
index 248f4eb35..4c83addb8 100644
--- a/dsp/modules/lm.py
+++ b/dsp/modules/lm.py
@@ -33,7 +33,8 @@ class LM(ABC):
def inspect_history(self, n: int = 1, skip: int = 0):
"""Prints the last n prompts and their completions.
- TODO: print the valid choice that contains filled output field instead of the first
+
+ TODO: print the valid choice that contains filled output field instead of the first.
"""
provider: str = self.provider
@@ -46,29 +47,17 @@ class LM(ABC):
if prompt != last_prompt:
- if provider == "clarifai" or provider == "google" or provider == "claude":
- printed.append(
- (
- prompt,
- x['response'],
- ),
- )
+ if provider == "clarifai" or provider == "google":
+ printed.append((prompt, x["response"]))
+ elif provider == "anthropic":
+ blocks = [{"text": block.text} for block in x["response"].content if block.type == "text"]
+ printed.append((prompt, blocks))
+ elif provider == "cohere":
+ printed.append((prompt, x["response"].generations))
elif provider == "mistral":
- printed.append(
- (
- prompt,
- x['response'].choices,
- ),
- )
+ printed.append((prompt, x['response'].choices))
else:
- printed.append(
- (
- prompt,
- x["response"].generations
- if provider == "cohere"
- else x["response"]["choices"],
- ),
- )
+ printed.append((prompt, x["response"]["choices"]))
last_prompt = prompt
@@ -86,9 +75,9 @@ class LM(ABC):
if provider == "cohere":
text = choices[0].text
elif provider == "openai" or provider == "ollama":
- text = ' ' + self._get_choice_text(choices[0]).strip()
- elif provider == "clarifai" or provider == "claude" :
- text=choices
+ text = " " + self._get_choice_text(choices[0]).strip()
+ elif provider == "clarifai":
+ text = choices
elif provider == "google":
text = choices[0].parts[0].text
elif provider == "mistral":
@@ -108,6 +97,6 @@ class LM(ABC):
def copy(self, **kwargs):
"""Returns a copy of the language model with the same parameters."""
kwargs = {**self.kwargs, **kwargs}
- model = kwargs.pop('model')
+ model = kwargs.pop("model")
return self.__class__(model=model, **kwargs)