Skip to content

Commit cbd34aa

Browse files
authored
Added support for RemoteModel with image scope (#14)
1 parent 2a56aa4 commit cbd34aa

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

ai_feedback/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def main() -> int:
198198

199199
if args.scope == "image":
200200
prompt["prompt_content"] = prompt_content
201-
request, response = image_processing.process_image(args, prompt)
201+
request, response = image_processing.process_image(args, prompt, system_instructions)
202202
elif args.scope == "text":
203203
request, response = text_processing.process_text(args, prompt_content, system_instructions)
204204
else:

ai_feedback/image_processing.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .helpers.image_extractor import extract_images
1212
from .helpers.image_reader import *
1313
from .helpers.template_utils import render_prompt_template
14+
from .models.RemoteModel import RemoteModel
1415

1516

1617
def encode_image(image_path: os.PathLike) -> bytes:
@@ -86,7 +87,7 @@ def anthropic_call(message: Message, model: str) -> str | None:
8687
return message.content[0].text
8788

8889

89-
def process_image(args, prompt: dict) -> tuple[str, str]:
90+
def process_image(args, prompt: dict, system_instructions: str) -> tuple[str, str]:
9091
"""Generates feedback for an image submission.
9192
Returns the LLM prompt delivered and the returned response."""
9293
OUTPUT_DIRECTORY = "output_images"
@@ -152,6 +153,20 @@ def process_image(args, prompt: dict) -> tuple[str, str]:
152153
responses.append(openai_call(message, model="gpt-4o"))
153154
elif args.model == Models.CLAUDE.value:
154155
responses.append(anthropic_call(message, model="claude-3-7-sonnet-20250219"))
156+
elif args.model == Models.REMOTE.value:
157+
if args.remote_model:
158+
model = RemoteModel(model_name=args.remote_model)
159+
else:
160+
model = RemoteModel()
161+
162+
_request, response = model.generate_response(
163+
rendered_prompt,
164+
args.submission,
165+
system_instructions=system_instructions,
166+
question_num=question,
167+
submission_image=args.submission_image,
168+
)
169+
responses.append(str(response))
155170
else:
156171
responses.append(chat(model=args.model, messages=[message], options={"temperature": 0.33}).message.content)
157172

ai_feedback/models/RemoteModel.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import json
21
import os
32
import re
43
import sys
5-
import urllib.request
64
from pathlib import Path
7-
from typing import List, Optional, Tuple
5+
from typing import Optional, Tuple
86

7+
import requests
98
from dotenv import load_dotenv
109

1110
from .Model import Model
@@ -39,6 +38,7 @@ def generate_response(
3938
test_output: Optional[Path] = None,
4039
scope: Optional[str] = None,
4140
llama_mode: Optional[str] = None,
41+
submission_image: Optional[str] = None,
4242
) -> Optional[Tuple[str, str]]:
4343
"""
4444
Generate a model response using the prompt and assignment files.
@@ -52,24 +52,21 @@ def generate_response(
5252
scope (Optional[str]): The scope to use for generating the response.
5353
system_instructions (str): instructions for the model
5454
llama_mode (Optional[str]): Optional mode to invoke llama.cpp in.
55+
submission_image (Optional[str]): An optional path to a submission image file.
5556
5657
Returns:
5758
Optional[Tuple[str, str]]: A tuple containing the prompt and the model's response,
5859
or None if the response was invalid.
5960
"""
6061
load_dotenv()
6162

62-
headers = {"X-API-KEY": os.getenv("REMOTE_API_KEY"), "Content-Type": "application/json"}
63+
headers = {"X-API-KEY": os.getenv("REMOTE_API_KEY")}
6364
data = {"content": prompt, "model": self.model_name, "system_instructions": system_instructions}
65+
files = {}
66+
if submission_image:
67+
filename = os.path.basename(submission_image)
68+
files[filename] = (filename, open(submission_image, 'rb'))
6469

65-
# Convert the data to JSON format
66-
json_data = json.dumps(data).encode("utf-8")
70+
response = requests.post(self.remote_url, data=data, headers=headers, files=files)
6771

68-
# Create the request
69-
request = urllib.request.Request(self.remote_url, data=json_data, headers=headers, method="POST")
70-
71-
# Send the request and get the response
72-
with urllib.request.urlopen(request) as response:
73-
response = json.loads(response.read().decode())
74-
75-
return request, response
72+
return prompt, response.json()

0 commit comments

Comments
 (0)