-
Notifications
You must be signed in to change notification settings - Fork 173
/
custom_provider_cohere.py
55 lines (51 loc) · 1.85 KB
/
custom_provider_cohere.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
A simple custom model provider to add to the ChainForge interface,
to support Cohere AI text completions through their Python API.
NOTE: You must have the `cohere` package installed and an API key.
"""
from chainforge.providers import provider
import cohere
# Init the Cohere client (replace with your Cohere API Key)
co = cohere.Client('<YOUR_API_KEY>')
# JSON schemas to pass react-jsonschema-form, one for this endpoints' settings and one to describe the settings UI.
COHERE_SETTINGS_SCHEMA = {
"settings": {
"temperature": {
"type": "number",
"title": "temperature",
"description": "Controls the 'creativity' or randomness of the response.",
"default": 0.75,
"minimum": 0,
"maximum": 5.0,
"multipleOf": 0.01,
},
"max_tokens": {
"type": "integer",
"title": "max_tokens",
"description": "Maximum number of tokens to generate in the response.",
"default": 100,
"minimum": 1,
"maximum": 1024,
},
},
"ui": {
"temperature": {
"ui:help": "Defaults to 1.0.",
"ui:widget": "range"
},
"max_tokens": {
"ui:help": "Defaults to 100.",
"ui:widget": "range"
},
}
}
# Our custom model provider for Cohere's text generation API.
@provider(name="Cohere",
emoji="🖇",
models=['command', 'command-nightly', 'command-light', 'command-light-nightly'],
rate_limit="sequential", # enter "sequential" for blocking; an integer N > 0 means N is the max mumber of requests per minute.
settings_schema=COHERE_SETTINGS_SCHEMA)
def CohereCompletion(prompt: str, model: str, temperature: float = 0.75, **kwargs) -> str:
print(f"Calling Cohere model {model} with prompt '{prompt}'...")
response = co.generate(model=model, prompt=prompt, temperature=temperature, **kwargs)
return response.generations[0].text