-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcohere_adapter.py
More file actions
40 lines (31 loc) · 1.06 KB
/
cohere_adapter.py
File metadata and controls
40 lines (31 loc) · 1.06 KB
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
import requests
def get_cohere_response(prompt,action):
cohere_api_key = 'CWDmyCR7osjXqlLmvGI1EPt5qN7iAsG3B3xjbFCj'
url = f'https://api.cohere.ai/v1/{action}'
print(url)
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {cohere_api_key}'
}
if action == 'summarize':
data = {
'text': prompt,
'length': 'auto',
}
else:
data = {
'prompt': prompt,
'num_completions': 5,
}
response = requests.post(url, json=data, headers=headers,verify=False)
response_json = response.json()
print(response_json)
if response.status_code == 200:
generations = response_json.get('generations', [])
if generations:
return generations[0].get('text', '')
else:
return 'Error: Failed to retrieve response from Cohere API'
else:
error_message = response_json.get('message', 'Unknown error occurred')
return f'Error: {error_message}'