Skip to content

Commit

Permalink
Switch to gpt-4-turbo, including for vision - closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 10, 2024
1 parent cc2d3b6 commit 66332e1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -31,7 +31,9 @@ plugins:

## Usage

Once installed, this plugin will allow users to select rows to enrich and run them through prompts using `gpt-3.5-turbo`, saving the result of the prompt in the specified column.
Once installed, this plugin will allow users to select rows to enrich and run them through prompts using `gpt-3.5-turbo` or `gpt-4-turbo`, saving the result of the prompt in the specified column.

The plugin also provides `gpt-4-turbo vision`, which can run prompts against an image identified by a URL.

## Development

Expand Down
13 changes: 5 additions & 8 deletions datasette_enrichments_gpt/__init__.py
Expand Up @@ -46,8 +46,8 @@ class ConfigForm(Form):
"Model",
choices=[
("gpt-3.5-turbo", "gpt-3.5-turbo"),
("gpt-4-1106-preview", "gpt-4-turbo"),
("gpt-4-vision-preview", "gpt-4-vision"),
("gpt-4-turbo", "gpt-4-turbo"),
("gpt-4-vision", "gpt-4-turbo vision"),
],
default="gpt-3.5-turbo",
)
Expand Down Expand Up @@ -132,9 +132,7 @@ async def _chat_completion(
body = {"model": model, "messages": messages}
if json_format:
body["response_format"] = {"type": "json_object"}
# Bump up max tokens on gpt-4-vision-preview
if model == "gpt-4-vision-preview":
body["max_tokens"] = 1000
body["max_tokens"] = 1000
async with httpx.AsyncClient() as client:
response = await client.post(
"https://api.openai.com/v1/chat/completions",
Expand Down Expand Up @@ -166,7 +164,6 @@ async def turbo_completion(
async def gpt4_vision(self, api_key, prompt, image_url, system=None) -> str:
messages = []
if system:
# TODO: Check that gpt-4-vision-preview supports system prompts
messages.append({"role": "system", "content": system})
messages.append(
{
Expand All @@ -177,7 +174,7 @@ async def gpt4_vision(self, api_key, prompt, image_url, system=None) -> str:
],
}
)
return await self._chat_completion(api_key, "gpt-4-vision-preview", messages)
return await self._chat_completion(api_key, "gpt-4-turbo", messages)

async def enrich_batch(
self,
Expand Down Expand Up @@ -209,7 +206,7 @@ async def enrich_batch(
"{{ %s }}" % key, str(value or "")
).replace("{{%s}}" % key, str(value or ""))
model = config["model"]
if model == "gpt-4-vision-preview":
if model == "gpt-4-vision":
output = await self.gpt4_vision(api_key, prompt, image_url, system)
else:
output = await self.turbo_completion(
Expand Down
2 changes: 1 addition & 1 deletion datasette_enrichments_gpt/templates/enrichment-gpt.html
Expand Up @@ -9,7 +9,7 @@
const divForJsonFormatInput = document.querySelector('#json_format').parentNode;
function handleModelChange() {
// If gpt-4-vision-preview, hide JSON, show image URL
if (modelSelect.value === 'gpt-4-vision-preview') {
if (modelSelect.value === 'gpt-4-vision') {
divForImageUrlLabel.style.display = '';
divForImageUrlInput.style.display = '';
divForJsonFormatLabel.style.display = 'none';
Expand Down

0 comments on commit 66332e1

Please sign in to comment.