harbor run -t openthoughts/tasktrove-mix-h2-language-balanced-v2-mix-h2-lang-balanced-2103harbor download openthoughts/tasktrove-mix-h2-language-balanced-v2-mix-h2-lang-balanced-2103solution file
import * as dotenv from 'dotenv'
import { readFile as rd } from 'fs'
import { promisify } from 'util'
import { ChatGPTAPI, ChatGPTError, ChatMessage, SendMessageOptions } from 'chatgpt'
import pRetry, { AbortError, FailedAttemptError } from 'p-retry'
import { extractJsonArray, extractCodeBlock, extractPostOutlineFromCodeBlock, extractSeoInfo, extractAudienceIntentInfo } from './extractor'
import {
getPromptForMainKeyword,
getPromptForOutline,
getPromptForIntroduction,
getPromptForHeading,
getPromptForConclusion,
getAutoSystemPrompt,
getPromptForSeoInfo,
getCustomSystemPrompt,
getSeoSystemPrompt,
getPromptForIntentAudience as getPromptForAudienceIntent
} from './prompts'
import {
Heading,
PostOutline,
PostPrompt,
TotalTokens,
SeoInfo
} from '../types'
import { encode } from './tokenizer'
import { extractPrompts } from './template'
import { log } from 'console'
import { NoApiKeyError } from './errors'
dotenv.config()
const readFile = promisify(rd)
/**
* Specific Open AI API parameters for the completion
*/
export type CompletionParams = {
temperature?: number | null,
top_p?: number | null,
max_tokens?: number,
presence_penalty?: number | null,
frequency_penalty?: number | null,
logit_bias?: object | null,
}
/**
* Interface for the helper class for generating a post. it defines how to generate a post
* Each helper class must implement this interface
* @interface
*/
export interface GeneratorHelperInterface {
init () : Promise<void>
isCustom() : boolean
generateContentOutline () : Promise<PostOutline>
generateMainKeyword () : Promise<string[]>
generateIntroduction () : Promise<string>
generateConclusion () : Promise<string>
generateHeadingContents (tableOfContent : PostOutline) : Promise<string>
generateCustomPrompt(prompt : string) : Promise<string>
generateSeoInfo () : Promise<SeoInfo>
getTotalTokens() : TotalTokens
getPrompt() : PostPrompt
}
/**
* Helper implementation for generating a post using the ChatGPT API
* @class
*/
export class ChatGptHelper implements GeneratorHelperInterface {
private postPrompt : PostPrompt
private api : ChatGPTAPI
// The parent message is either the previous one in the conversation (if a template is used)
// or the generated outline (if we are in auto mode)
private chatParentMessage : ChatMessage
private completionParams : CompletionParams
private totalTokens : TotalTokens = {
promptTokens: 0,
completionTokens: 0,
total: 0
}
// -----------------------------------------------
// CONSTRUCTOR AND INITIALIZATION
// -----------------------------------------------
public constructor (postPrompt : PostPrompt) {
this.postPrompt = postPrompt
}
isCustom () : boolean {
return this.postPrompt?.templateFile !== undefined
}
getPrompt (): PostPrompt {
return this.postPrompt
}
getTotalTokens (): TotalTokens {
return this.totalTokens
}
async init () {
if (this.isCustom()) {
if (this.postPrompt.debug) {
console.log(`Use template : ${this.postPrompt.templateFile}`)
}
this.postPrompt.templateContent = await this.readTemplate()
this.postPrompt.prompts = extractPrompts(this.postPrompt.templateContent)
}
const systemMessage = this.isCustom() ? getCustomSystemPrompt(this.postPrompt) : getAutoSystemPrompt(this.postPrompt)
await this.buildChatGPTAPI(systemMessage)
}
private async buildChatGPTAPI (systemMessage : string) {
try {
this.api = new ChatGPTAPI({
apiKey: this.postPrompt?.apiKey || process.env.OPENAI_API_KEY,
completionParams: {
model: this.postPrompt.model
},
systemMessage,
debug: this.postPrompt.debugapi
})
} catch (error) {
throw new NoApiKeyError()
}
if (this.postPrompt.debug) {
console.log(`OpenAI API initialized with model : ${this.postPrompt.model}`)
}
this.completionParams = {
temperature: this.postPrompt.temperature ?? 0.8,
frequency_penalty: this.postPrompt.frequencyPenalty ?? 0,
presence_penalty: this.postPrompt.presencePenalty ?? 1
}
if (this.postPrompt.logitBias) {
const mainKwWords = await this.generateMainKeyword()
// set the logit bias in order to force the model to minimize the usage of the main keyword
const logitBiais: Record<number, number> = {}
mainKwWords.forEach((kw) => {
const encoded = encode(kw)
encoded.forEach((element) => {
logitBiais[element] = Number(this.postPrompt.logitBias) || -1
})
})
this.completionParams.logit_bias = logitBiais
}
if (this.postPrompt.debug) {
console.log('---------- COMPLETION PARAMETERS ----------')
console.log('Max Tokens : ' + this.completionParams.max_tokens)
console.log('Temperature : ' + this.completionParams.temperature)
console.log('Frequency Penalty : ' + this.completionParams.frequency_penalty)
console.log('Presence Penalty : ' + this.completionParams.presence_penalty)
console.log('Logit Biais : ' + this.completionParams.logit_bias)
}
}
// -----------------------------------------------
// METHODS FOR THE AUTOMATIC MODE
// -----------------------------------------------
async generateMainKeyword () {
const prompt = getPromptForMainKeyword()
if (this.postPrompt.debug) {
console.log('---------- PROMPT MAIN KEYWORD ----------')
console.log(prompt)
}
const response = await this.sendRequest(prompt)
if (this.postPrompt.debug) {
console.log('---------- MAIN KEYWORD ----------')
console.log(response.text)
}
return extractJsonArray(response.text)
}
async generateContentOutline () {
if (this.postPrompt.generate) {
const audienceIntent = await this.generateAudienceIntent()
this.postPrompt = {
...audienceIntent,
...this.postPrompt
}
}
const prompt = getPromptForOutline(this.postPrompt)
if (this.postPrompt.debug) {
console.log('---------- PROMPT OUTLINE ----------')
console.log(prompt)
}
// the parent message is the outline for the upcoming content
// By this way, we can decrease the cost of the API call by minimizing the number of prompt tokens
// TODO : add an option to disable this feature
this.chatParentMessage = await this.sendRequest(prompt)
if (this.postPrompt.debug) {
console.log('---------- OUTLINE ----------')
console.log(this.chatParentMessage.text)
}
return extractPostOutlineFromCodeBlock(this.chatParentMessage.text)
}
async generateAudienceIntent () {
const prompt = getPromptForAudienceIntent(this.postPrompt)
if (this.postPrompt.debug) {
console.log('---------- PROMPT AUDIENCE INTENT ----------')
console.log(prompt)
}
const response = await this.sendRequest(prompt)
if (this.postPrompt.debug) {
console.log('---------- AUDIENCE INTENT ----------')
console.log(response.text)
}
return extractAudienceIntentInfo(response.text)
}
async generateIntroduction () {
const response = await this.sendRequest(getPromptForIntroduction(this.postPrompt), this.completionParams)
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py