-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.py
120 lines (98 loc) · 3.76 KB
/
cli.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python
import boto3
import json
from typing import Optional
def get_bedrock_claude_response(prompt: str,
temperature: float = 0.7,
max_tokens: Optional[int] = None) -> str:
"""
Call Claude 3 Sonnet on AWS Bedrock and get a response.
Args:
prompt (str): The input prompt to send to the API
temperature (float): Controls randomness (0-1, default: 0.7)
max_tokens (int, optional): Maximum tokens in response
Returns:
str: The API response text
"""
try:
# Initialize Bedrock client
bedrock = boto3.client('bedrock-runtime')
# Prepare the request body
request_body = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": max_tokens if max_tokens else 4096,
"messages": [
{"role": "user", "content": prompt}
],
"temperature": temperature
}
# Call Bedrock
response = bedrock.invoke_model(
modelId="us.anthropic.claude-3-5-sonnet-20240620-v1:0",
body=json.dumps(request_body)
)
# Parse the response
response_body = json.loads(response['body'].read())
return response_body['content'][0]['text']
except Exception as e:
print(f"Error calling Bedrock API: {str(e)}")
return ""
def get_openai_response(prompt: str,
model: str = "gpt-3.5-turbo",
temperature: float = 0.7,
max_tokens: Optional[int] = None) -> str:
"""
Call OpenAI API and get a response.
Args:
prompt (str): The input prompt to send to the API
model (str): The model to use (default: gpt-3.5-turbo)
temperature (float): Controls randomness (0-1, default: 0.7)
max_tokens (int, optional): Maximum tokens in response
Returns:
str: The API response text
"""
try:
# Initialize the client
client = OpenAI()
# Create the chat completion with the new client
response = client.chat.completions.create(
model=model,
messages=[
{"role": "user", "content": prompt}
],
temperature=temperature,
max_tokens=max_tokens
)
return response.choices[0].message.content.strip()
except Exception as e:
print(f"Error calling OpenAI API: {str(e)}")
return ""
def main():
import argparse
parser = argparse.ArgumentParser(description='Get responses from OpenAI or Bedrock')
parser.add_argument('--provider', type=str, choices=['openai', 'bedrock'],
default='bedrock', help='The API provider to use')
parser.add_argument('prompt', type=str, help='The input prompt to send to the API')
parser.add_argument('--model', type=str, default='gpt-3.5-turbo',
help='The model to use (default: gpt-3.5-turbo)')
parser.add_argument('--temperature', type=float, default=0.7,
help='Controls randomness (0-1, default: 0.7)')
parser.add_argument('--max-tokens', type=int,
help='Maximum tokens in response')
args = parser.parse_args()
if args.provider == 'openai':
response = get_openai_response(
prompt=args.prompt,
model=args.model,
temperature=args.temperature,
max_tokens=args.max_tokens
)
else:
response = get_bedrock_claude_response(
prompt=args.prompt,
temperature=args.temperature,
max_tokens=args.max_tokens
)
print(response)
if __name__ == '__main__':
main()