-
Notifications
You must be signed in to change notification settings - Fork 16
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
Showing
5 changed files
with
219 additions
and
3 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
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,64 @@ | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
import { capture } from '@snapshot-labs/snapshot-sentry'; | ||
import { URL } from 'url'; | ||
import { rpcError, fetchWithKeepAlive } from '../helpers/utils'; | ||
Check failure on line 5 in src/lib/ai.ts GitHub Actions / lint (18) / Lint
|
||
import OpenAI from 'openai'; | ||
import { fetchProposal } from '../helpers/snapshot'; | ||
import { IStorage } from './storage/types'; | ||
import { Proposal } from '../helpers/snapshot'; | ||
import Cache from './cache'; | ||
|
||
const openai = new OpenAI({ apiKey: process.env.apiKey }); | ||
|
||
class AISummary extends Cache { | ||
proposal?: Proposal | null; | ||
|
||
constructor(id: string, storage: IStorage) { | ||
super(id, storage); | ||
this.filename = `snapshot-votes-report-${this.id}.csv`; | ||
} | ||
|
||
async isCacheable() { | ||
this.proposal = await fetchProposal(this.id); | ||
console.log(this.proposal); | ||
|
||
if (!this.proposal) { | ||
return Promise.reject('RECORD_NOT_FOUND'); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
getContent = async () => { | ||
this.isCacheable(); | ||
console.log("--ID: " + this.id); | ||
try { | ||
const proposal = await fetchProposal(this.id); | ||
const body = proposal?.body; | ||
const title = proposal?.title; | ||
const spaceName = proposal?.space?.name; | ||
const completion = await openai.chat.completions.create({ | ||
messages: [{ role: 'system', content: `The following is a governance proposal of ${spaceName}. Generate me a summary in 300 characters max. Here's the title of the proposal: '${title}' . Here's the content of the proposal: '${body}'` }], | ||
model: 'gpt-4-turbo-preview', | ||
}); | ||
|
||
if (completion.choices.length === 0) { | ||
throw new Error('No completion in response'); | ||
} | ||
const res = completion?.choices[0]; | ||
|
||
if (res.message.content === null) { | ||
throw new Error('No content in response'); | ||
} | ||
|
||
const content = res.message?.content; | ||
return content; | ||
} catch (e: any) { | ||
capture(e); | ||
throw (e); | ||
} | ||
}; | ||
} | ||
|
||
export default AISummary; |
Oops, something went wrong.