zaldivards commited on
Commit
694a9b2
·
1 Parent(s): 7c53c52

Add functions to invoke Bedrock and OpenAI models

Browse files
Files changed (1) hide show
  1. utils.py +22 -0
utils.py CHANGED
@@ -1,14 +1,36 @@
1
  import os
 
2
  from io import BytesIO
3
 
4
  import boto3
5
  import requests
 
6
 
7
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
8
  BEDROCK_MODEL_ID = "anthropic.claude-3-5-sonnet-20240620-v1:0"
9
  OPENAI_MODEL_ID = "o4-mini"
 
10
 
11
  bedrock_runtime = boto3.client("bedrock-runtime", region_name=os.getenv("AWS_REGION"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
 
14
  def get_file(task_id: str) -> BytesIO:
 
1
  import os
2
+ import json
3
  from io import BytesIO
4
 
5
  import boto3
6
  import requests
7
+ from openai import OpenAI
8
 
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
  BEDROCK_MODEL_ID = "anthropic.claude-3-5-sonnet-20240620-v1:0"
11
  OPENAI_MODEL_ID = "o4-mini"
12
+ OPENAI_MODEL_IMAGE_MODEL_ID = "gpt-4.1-mini"
13
 
14
  bedrock_runtime = boto3.client("bedrock-runtime", region_name=os.getenv("AWS_REGION"))
15
+ openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
16
+
17
+
18
+ def invoke_bedrock_model(messages: list[dict]) -> str:
19
+ """Invokes a Bedrock model with the provided messages."""
20
+ response = bedrock_runtime.invoke_model(
21
+ modelId=BEDROCK_MODEL_ID,
22
+ body=json.dumps({"anthropic_version": "bedrock-2023-05-31", "max_tokens": 4096, "messages": messages}),
23
+ )["body"].read()
24
+ return json.loads(response)["content"][0]["text"]
25
+
26
+
27
+ def invoke_openai_model(messages: list[dict]) -> str:
28
+ """Invokes an OpenAI model with the provided messages."""
29
+ response = openai_client.responses.create(
30
+ model=OPENAI_MODEL_IMAGE_MODEL_ID,
31
+ input=messages,
32
+ )
33
+ return response.output_text
34
 
35
 
36
  def get_file(task_id: str) -> BytesIO: