-
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.
Added claude 3.5 sonnet and perplexity with llama model to curated ai…
… models list
- Loading branch information
1 parent
7ee9936
commit b851009
Showing
6 changed files
with
162 additions
and
0 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
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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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 content = response.content[0]; | ||
|
||
if (content.type !== 'text') { | ||
throw new Error('Unexpected response type from Claude'); | ||
} | ||
|
||
const parsedResponse = JSON.parse(content.text); | ||
|
||
// 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, | ||
}; | ||
} catch (error) { | ||
console.error('Error in Claude 3.5 Sonnet handler:', error); | ||
throw new Error('Failed to process Claude 3.5 Sonnet response'); | ||
} | ||
}; |
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
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { z } from 'zod'; | ||
import { ModelHandler } from './index'; | ||
|
||
const PerplexityResponseSchema = z.object({ | ||
id: z.string(), | ||
model: z.string(), | ||
object: z.string(), | ||
created: z.number(), | ||
choices: z.array( | ||
z.object({ | ||
index: z.number(), | ||
finish_reason: z.string(), | ||
message: z.object({ | ||
role: z.string(), | ||
content: z.string(), | ||
}), | ||
delta: z.object({ | ||
role: z.string(), | ||
content: z.string(), | ||
}), | ||
}) | ||
), | ||
usage: z.object({ | ||
prompt_tokens: z.number(), | ||
completion_tokens: z.number(), | ||
total_tokens: z.number(), | ||
}), | ||
}); | ||
|
||
const GameResponseSchema = z.object({ | ||
reasoning: z.string(), | ||
playerCoordinates: z.array(z.number()), | ||
boxCoordinates: z.array(z.array(z.number())), | ||
}); | ||
|
||
export const perplexityModel: ModelHandler = async (prompt: string, map: string[][]) => { | ||
const apiKey = process.env.PERPLEXITY_API_KEY; | ||
if (!apiKey) { | ||
throw new Error('PERPLEXITY_API_KEY is not set in the environment variables'); | ||
} | ||
|
||
const messages = [ | ||
{ role: 'system', content: 'Be precise and concise.' }, | ||
{ role: 'user', content: prompt }, | ||
{ role: 'user', content: JSON.stringify(map) }, | ||
]; | ||
|
||
const options: RequestInit = { | ||
method: 'POST', | ||
headers: { | ||
'Authorization': `Bearer ${apiKey}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
model: 'llama-3.1-sonar-large-128k-online', | ||
messages, | ||
temperature: 0.2, | ||
top_p: 0.9, | ||
return_citations: true, | ||
search_domain_filter: ['perplexity.ai'], | ||
return_images: false, | ||
return_related_questions: false, | ||
search_recency_filter: 'month', | ||
top_k: 0, | ||
stream: false, | ||
presence_penalty: 0, | ||
frequency_penalty: 1, | ||
}), | ||
}; | ||
|
||
try { | ||
const response = await fetch('https://api.perplexity.ai/chat/completions', options); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
const data = await response.json(); | ||
|
||
const validatedResponse = PerplexityResponseSchema.parse(data); | ||
const content = validatedResponse.choices[0].message.content; | ||
|
||
const parsedContent = JSON.parse(content); | ||
const gameResponse = GameResponseSchema.parse(parsedContent); | ||
|
||
return { | ||
boxCoordinates: gameResponse.boxCoordinates, | ||
playerCoordinates: gameResponse.playerCoordinates, | ||
reasoning: gameResponse.reasoning, | ||
}; | ||
} catch (error) { | ||
console.error('Error:', error); | ||
throw new Error('Failed to run Perplexity model'); | ||
} | ||
}; |
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