-
Notifications
You must be signed in to change notification settings - Fork 3
Add json schema to compatible models #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
2dfebbb
styles
9a41cdf
styles
4e4e5dd
testing structured output for ollama
88f1001
test
ae9a93c
test
1067871
styles
1609ee6
updated the models to use json dumps
c85bd57
upgrade OpenAIModel to gpt-4o-mini
38c0326
updated OpenAIModel to use structured response
19e651e
updated models and annotations to be compatible with new openai model
dea83b9
styling
6c8cfb1
updated codellama and deepseekv3 to work with new style of annotations
99af119
updated deepseekv3 cli mode to work with new schema format
cd54e69
updated deepseekv3 server payload
4618174
change how payload recives schema
d0a7cef
stlyes
964161b
updated readme
0e79f7c
updated readme
18567c2
updated model descriptions
d8f75e4
updated model descriptions
bfb060d
updated model descriptions
6c4d8d8
Merge branch 'main' into add-json-schema
wkukka1 64df9d5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1fa1146
styles
f24a1e0
Merge branch 'add-json-schema' of github.com:wkukka1/ai-autograding-f…
8b489a8
added fallback for schema in ollama models
93dbd4b
updated helper to use json_schema
2b8d924
updated models to have consistent json shcema file checks
0765342
Merge branch 'main' into add-json-schema
david-yz-liu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| { | ||
| "name": "student_code_annotation", | ||
| "description": "List of code annotations describing specific mistakes in the student's code.", | ||
| "schema": { | ||
| "type": "object", | ||
| "properties": { | ||
| "annotations": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "object", | ||
| "properties": { | ||
| "filename": { | ||
| "type": "string", | ||
| "description": "The name of the student's file where the issue was found." | ||
| }, | ||
| "content": { | ||
| "type": "string", | ||
| "description": "A short description of the mistake or issue." | ||
| }, | ||
| "line_start": { | ||
| "type": "integer", | ||
| "description": "The starting line number where the issue begins.", | ||
| "minimum": 1 | ||
| }, | ||
| "line_end": { | ||
| "type": "integer", | ||
| "description": "The ending line number where the issue ends.", | ||
| "minimum": 1 | ||
| }, | ||
| "column_start": { | ||
| "type": "integer", | ||
| "description": "The starting column position of the mistake.", | ||
| "minimum": 0 | ||
| }, | ||
| "column_end": { | ||
| "type": "integer", | ||
| "description": "The ending column position of the mistake.", | ||
| "minimum": 0 | ||
| } | ||
| }, | ||
| "required": [ | ||
| "filename", | ||
| "content", | ||
| "line_start", | ||
| "line_end", | ||
| "column_start", | ||
| "column_end" | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| "required": ["annotations"] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import json | ||
| import os | ||
| import re | ||
| from pathlib import Path | ||
| from typing import Optional, Tuple | ||
|
|
||
|
|
@@ -30,6 +32,7 @@ def generate_response( | |
| test_output: Optional[Path] = None, | ||
| scope: Optional[str] = None, | ||
| llama_mode: Optional[str] = None, | ||
| json_schema: Optional[str] = None, | ||
| ) -> Tuple[str, str]: | ||
| """ | ||
| Generate a response based on the given prompt and assignment context. | ||
|
|
@@ -43,30 +46,47 @@ def generate_response( | |
| question_num (Optional[int]): Specific question number to focus on. | ||
| system_instructions (str): instructions for the model | ||
| llama_mode (Optional[str]): Optional mode to invoke llama.cpp in. | ||
| json_schema (Optional[str]): Optional json schema to use. | ||
| Returns: | ||
| Tuple[str, str]: The full prompt and the generated response from OpenAI. | ||
| """ | ||
| response = self._call_openai(prompt, system_instructions) | ||
| if json_schema: | ||
| schema_path = Path(json_schema) | ||
| if not schema_path.exists(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a nice check; please add this to all of the classes. |
||
| raise FileNotFoundError(f"JSON schema file not found: {schema_path}") | ||
| with open(schema_path, "r", encoding="utf-8") as f: | ||
| schema = json.load(f) | ||
| else: | ||
| schema = None | ||
|
|
||
| response = self._call_openai(prompt, system_instructions, schema) | ||
| return prompt, response | ||
|
|
||
| def _call_openai(self, prompt: str, system_instructions: str) -> str: | ||
| def _call_openai(self, prompt: str, system_instructions: str, schema: Optional[dict] = None) -> str: | ||
| """ | ||
| Send a prompt to OpenAI's chat completion API and retrieve the generated response. | ||
| Args: | ||
| prompt (str): The fully constructed input prompt including file content. | ||
| schema (Optional[dict]): Optional json schema to use. | ||
| Returns: | ||
| str: The model's response text. | ||
| """ | ||
| response_format = None | ||
| if schema: | ||
| response_format = {"type": "json_schema", "json_schema": schema} | ||
|
|
||
| response = self.client.chat.completions.create( | ||
| model="gpt-4-turbo", | ||
| model="gpt-4o-mini", | ||
| messages=[ | ||
| {"role": "system", "content": system_instructions}, | ||
| {"role": "user", "content": prompt}, | ||
| ], | ||
| max_tokens=1000, | ||
| response_format=response_format, | ||
| temperature=0.5, | ||
| max_tokens=1000, | ||
| ) | ||
|
|
||
| return response.choices[0].message.content | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the code structure consistent with the other classes (if-else, etc.) Also keep the
opencall the same, with theutf-8encoding