-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b851009
commit 0a43029
Showing
1 changed file
with
35 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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,47 +1,44 @@ | ||
import { Anthropic } from '@anthropic-ai/sdk'; | ||
import { Anthropic } from "@anthropic-ai/sdk"; | ||
import { type ModelHandler } from "."; | ||
|
||
const anthropic = new Anthropic({ | ||
apiKey: process.env.ANTHROPIC_API_KEY, | ||
}); | ||
|
||
export const claude35sonnet: ModelHandler = async (prompt, map) => { | ||
try { | ||
const response = await anthropic.messages.create({ | ||
model: "claude-3-sonnet-20240307", | ||
max_tokens: 1024, | ||
temperature: 0, | ||
system: prompt, | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: JSON.stringify(map), | ||
}, | ||
], | ||
}); | ||
const anthropic = new Anthropic({ | ||
apiKey: process.env.ANTHROPIC_API_KEY, | ||
}); | ||
|
||
const content = response.content[0]; | ||
const response = await anthropic.messages.create({ | ||
model: "claude-3-sonnet-20240307", | ||
max_tokens: 1024, | ||
temperature: 0, | ||
system: prompt, | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: JSON.stringify(map), | ||
}, | ||
], | ||
}); | ||
|
||
if (content.type !== 'text') { | ||
throw new Error('Unexpected response type from Claude'); | ||
} | ||
const content = response.content[0]; | ||
|
||
const parsedResponse = JSON.parse(content.text); | ||
if (content.type !== "text") { | ||
throw new Error("Unexpected response type from Claude"); | ||
} | ||
|
||
// Validate the response structure | ||
if (!Array.isArray(parsedResponse.boxCoordinates) || | ||
!Array.isArray(parsedResponse.playerCoordinates) || | ||
typeof parsedResponse.reasoning !== 'string') { | ||
throw new Error('Invalid response structure'); | ||
} | ||
const parsedResponse = JSON.parse(content.text); | ||
|
||
return { | ||
boxCoordinates: parsedResponse.boxCoordinates, | ||
playerCoordinates: parsedResponse.playerCoordinates, | ||
reasoning: parsedResponse.reasoning, | ||
}; | ||
} catch (error) { | ||
console.error('Error in Claude 3.5 Sonnet handler:', error); | ||
throw new Error('Failed to process Claude 3.5 Sonnet response'); | ||
// Validate the response structure | ||
if ( | ||
!Array.isArray(parsedResponse.boxCoordinates) || | ||
!Array.isArray(parsedResponse.playerCoordinates) || | ||
typeof parsedResponse.reasoning !== "string" | ||
) { | ||
throw new Error("Invalid response structure"); | ||
} | ||
}; | ||
|
||
return { | ||
boxCoordinates: parsedResponse.boxCoordinates, | ||
playerCoordinates: parsedResponse.playerCoordinates, | ||
reasoning: parsedResponse.reasoning, | ||
}; | ||
}; |