Skip to content

Commit

Permalink
feat: add pnpm cache steps
Browse files Browse the repository at this point in the history
Signed-off-by: sarthakjdev <jsarthak448@gmail.com>
  • Loading branch information
sarthakjdev committed Jul 14, 2024
1 parent ecd0c59 commit 77eb80c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 57 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ jobs:
with:
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v4
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install Dependencies
run: pnpm install --frozen-lockfile

Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ jobs:
with:
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v4
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm i --frozen-lockfile

Expand Down
80 changes: 40 additions & 40 deletions apps/wapi-ai-chatbot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,52 @@ import { askAi } from './utils/gpt'
import { getCache, setCache } from './utils/cache'

async function init() {
try {
whatsappClient.on('Ready', () => {
console.log('Client is ready')
})

whatsappClient.on('Error', error => {
console.error('Error', error.message)
})

whatsappClient.on('TextMessage', async (event: TextMessageEvent) => {
const cachedResponse = await getCache(event.text.data.text)
console.log({ cachedResponse })
let response = 'Sorry, I am not able to understand that.'
if (cachedResponse) {
response = String(cachedResponse)
} else {
const aiResponse = await askAi(event.text.data.text)
response = aiResponse || response
if (aiResponse) {
setCache(event.text.data.text, aiResponse)
}
}

console.log({ response })
await event.reply({
message: new TextMessage({
text: response
})
})
})

whatsappClient.initiate()
} catch (error) {
console.error(error)
// ! TODO: you may prefer to send a notification to your slack channel or email here
}
try {
whatsappClient.on('Ready', () => {
console.log('Client is ready')
})

whatsappClient.on('Error', error => {
console.error('Error', error.message)
})

whatsappClient.on('TextMessage', async (event: TextMessageEvent) => {
const cachedResponse = await getCache(event.text.data.text)
console.log({ cachedResponse })
let response = 'Sorry, I am not able to understand that.'
if (cachedResponse) {
response = String(cachedResponse)
} else {
const aiResponse = await askAi(event.text.data.text)
response = aiResponse || response
if (aiResponse) {
setCache(event.text.data.text, aiResponse)
}
}

console.log({ response })
await event.reply({
message: new TextMessage({
text: response
})
})
})

whatsappClient.initiate()
} catch (error) {
console.error(error)
// ! TODO: you may prefer to send a notification to your slack channel or email here
}
}

init().catch(error => console.error(error))

process.on('unhandledRejection', error => {
console.error('unhandledRejection', error)
process.exit(1)
console.error('unhandledRejection', error)
process.exit(1)
})

process.on('uncaughtException', error => {
console.error('uncaughtException', error)
process.exit(1)
console.error('uncaughtException', error)
process.exit(1)
})
39 changes: 22 additions & 17 deletions apps/wapi-ai-chatbot/src/utils/gpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@ import { OpenAI } from 'openai'
const openAiApiKey = process.env.OPEN_AI_API_KEY

if (!openAiApiKey) {
throw new Error('OPEN_AI_API_KEY not defined!')
throw new Error('OPEN_AI_API_KEY not defined!')
}

const OpenApiClient = new OpenAI({
apiKey: openAiApiKey,
project: 'proj_viwVq5nzCR5Mj4TnIw4FQoNO',
organization: 'org-wwfaGhYXIA7CBUSg6x8DbKYj'
apiKey: openAiApiKey,
project: 'proj_viwVq5nzCR5Mj4TnIw4FQoNO',
organization: 'org-wwfaGhYXIA7CBUSg6x8DbKYj'
})

export async function askAi(message: string): Promise<string | null> {
try {
const chatCompletion = await OpenApiClient.chat.completions.create({
messages: [{ role: 'user', content: message }],
model: 'gpt-3.5-turbo'
})

console.log(JSON.stringify({ chatCompletion }))

return chatCompletion.choices[0].message.content
} catch (error) {
console.log({ error })
return null
}
try {
const chatCompletion = await OpenApiClient.chat.completions.create({
messages: [{ role: 'user', content: message }],
model: 'gpt-3.5-turbo'
})

console.log(JSON.stringify({ chatCompletion }))

return chatCompletion.choices[0].message.content
} catch (error) {
console.log({ error })
return null
}
}


export async function generateTranscription(){

}

0 comments on commit 77eb80c

Please sign in to comment.