-
Notifications
You must be signed in to change notification settings - Fork 159
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
f51e657
commit 825deef
Showing
7 changed files
with
197 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { APIError, APIHandler } from './helpers/endpoint' | ||
import { track } from 'shared/analytics' | ||
import { largePerplexityModel, perplexity } from 'shared/helpers/perplexity' | ||
import { models, promptClaude } from 'shared/helpers/claude' | ||
import { | ||
addAnswersModeDescription, | ||
multiChoiceOutcomeTypeDescriptions, | ||
} from 'common/ai-creation-prompts' | ||
import { log } from 'shared/utils' | ||
|
||
export const generateAIAnswers: APIHandler<'generate-ai-answers'> = async ( | ||
props, | ||
auth | ||
) => { | ||
const { question, shouldAnswersSumToOne, description, answers } = props | ||
const answersString = answers.filter(Boolean).join(', ') | ||
const prompt = `Question: ${question} ${ | ||
description && description !== '<p></p>' | ||
? `\nDescription: ${description}` | ||
: '' | ||
} ${ | ||
answersString.length | ||
? `\nHere are my suggested answers: ${answersString}` | ||
: '' | ||
}` | ||
log('generateAIAnswers prompt', prompt) | ||
const outcomeKey = shouldAnswersSumToOne | ||
? 'DEPENDENT_MULTIPLE_CHOICE' | ||
: 'INDEPENDENT_MULTIPLE_CHOICE' | ||
log('generateAIAnswers', { props }) | ||
try { | ||
// First use perplexity to research the topic | ||
const { messages, citations } = await perplexity(prompt, { | ||
model: largePerplexityModel, | ||
systemPrompts: [ | ||
`You are a helpful AI assistant that researches information to help generate possible answers for a multiple choice question.`, | ||
], | ||
}) | ||
|
||
const perplexityResponse = | ||
[messages].join('\n') + '\n\nSources:\n' + citations.join('\n\n') | ||
|
||
// Then use Claude to generate the answers | ||
const systemPrompt = ` | ||
You are a helpful AI assistant that generates possible answers for multiple choice prediction market questions. | ||
The question type is ${outcomeKey}. | ||
${multiChoiceOutcomeTypeDescriptions} | ||
Guidelines: | ||
- Generate 2-20 possible answers based on the research${ | ||
answersString.length | ||
? ` and the user's suggested answers: ${answersString}` | ||
: '' | ||
}, as well as a recommended addAnswersMode | ||
- Answers should be concise and clear | ||
- The addAnswersMode should be one of the following: | ||
${addAnswersModeDescription} | ||
${ | ||
answersString.length | ||
? `- Do NOT repeat any of the user's suggested answers, but DO match the style, idea, and range (if numeric) of the user's suggested answers, e.g. return answers of type 11-20, 21-30, etc. if the user suggests 1-10, or use 3-letter months if they suggest Feb, Mar, Apr, etc.` | ||
: '' | ||
} | ||
- ONLY return a single JSON object with "answers" string array and "addAnswersMode" string. Do not return anything else. | ||
Here is current information from the internet that is related to the question: | ||
${perplexityResponse} | ||
` | ||
|
||
const claudePrompt = `${prompt}\n\nReturn ONLY a JSON object containing "answers" string array and "addAnswersMode" string.` | ||
|
||
const claudeResponse = await promptClaude(claudePrompt, { | ||
model: models.sonnet, | ||
system: systemPrompt, | ||
}) | ||
log('claudeResponse', claudeResponse) | ||
|
||
const result = JSON.parse(claudeResponse) | ||
|
||
track(auth.uid, 'generate-ai-answers', { | ||
question, | ||
}) | ||
|
||
return result | ||
} catch (e) { | ||
console.error('Failed to generate answers:', e) | ||
throw new APIError(500, 'Failed to generate answers. Please try again.') | ||
} | ||
} |
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
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