|
| 1 | +"""Wrapper around Cohere APIs.""" |
| 2 | +import os |
| 3 | +from typing import Any, Dict, List, Optional |
| 4 | + |
| 5 | +from pydantic import BaseModel, Extra, root_validator |
| 6 | + |
| 7 | +from langchain.llms.base import LLM |
| 8 | + |
| 9 | + |
| 10 | +def remove_stop_tokens(text: str, stop: List[str]) -> str: |
| 11 | + """Remove stop tokens, should they occur at end.""" |
| 12 | + for s in stop: |
| 13 | + if text.endswith(s): |
| 14 | + return text[: -len(s)] |
| 15 | + return text |
| 16 | + |
| 17 | + |
| 18 | +class Cohere(BaseModel, LLM): |
| 19 | + """Wrapper around Cohere large language models.""" |
| 20 | + |
| 21 | + client: Any |
| 22 | + model: str = "gptd-instruct-tft" |
| 23 | + max_tokens: int = 256 |
| 24 | + temperature: float = 0.6 |
| 25 | + k: int = 0 |
| 26 | + p: int = 1 |
| 27 | + frequency_penalty: int = 0 |
| 28 | + presence_penalty: int = 0 |
| 29 | + |
| 30 | + class Config: |
| 31 | + """Configuration for this pydantic object.""" |
| 32 | + |
| 33 | + extra = Extra.forbid |
| 34 | + |
| 35 | + @root_validator() |
| 36 | + def template_is_valid(cls, values: Dict) -> Dict: |
| 37 | + """Validate that api key python package exists in environment.""" |
| 38 | + if "COHERE_API_KEY" not in os.environ: |
| 39 | + raise ValueError( |
| 40 | + "Did not find Cohere API key, please add an environment variable" |
| 41 | + " `COHERE_API_KEY` which contains it." |
| 42 | + ) |
| 43 | + try: |
| 44 | + import cohere |
| 45 | + |
| 46 | + values["client"] = cohere.Client(os.environ["COHERE_API_KEY"]) |
| 47 | + except ImportError: |
| 48 | + raise ValueError( |
| 49 | + "Could not import cohere python package. " |
| 50 | + "Please it install it with `pip install cohere`." |
| 51 | + ) |
| 52 | + return values |
| 53 | + |
| 54 | + def __call__(self, prompt: str, stop: Optional[List[str]] = None) -> str: |
| 55 | + """Call out to Cohere's generate endpoint.""" |
| 56 | + response = self.client.generate( |
| 57 | + model=self.model, |
| 58 | + prompt=prompt, |
| 59 | + max_tokens=self.max_tokens, |
| 60 | + temperature=self.temperature, |
| 61 | + k=self.k, |
| 62 | + p=self.p, |
| 63 | + frequency_penalty=self.frequency_penalty, |
| 64 | + presence_penalty=self.presence_penalty, |
| 65 | + stop_sequences=stop, |
| 66 | + ) |
| 67 | + text = response.generations[0].text |
| 68 | + # If stop tokens are provided, Cohere's endpoint returns them. |
| 69 | + # In order to make this consistent with other endpoints, we strip them. |
| 70 | + if stop is not None: |
| 71 | + text = remove_stop_tokens(text, stop) |
| 72 | + return text |
0 commit comments