Skip to content

Commit

Permalink
update azure dalle support
Browse files Browse the repository at this point in the history
  • Loading branch information
SchneeHertz committed Dec 1, 2023
1 parent 9ee819e commit 4c0f6fb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 26 deletions.
20 changes: 18 additions & 2 deletions modules/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const _ = require('lodash')

const { config: {
OPENAI_API_KEY, OPENAI_API_ENDPOINT, DEFAULT_MODEL,
AZURE_OPENAI_KEY, AZURE_OPENAI_ENDPOINT, AZURE_API_VERSION, AZURE_CHAT_MODEL, AZURE_EMBEDDING_MODEL,
AZURE_OPENAI_KEY, AZURE_OPENAI_ENDPOINT, AZURE_API_VERSION, AZURE_CHAT_MODEL, AZURE_EMBEDDING_MODEL, AZURE_IMAGE_MODEL,
useProxy, proxyObject
} } = require('../utils/loadConfig.js')
const proxyString = `${proxyObject.protocol}://${proxyObject.host}:${proxyObject.port}`
Expand Down Expand Up @@ -156,6 +156,21 @@ const azureOpenaiEmbedding = ({ input, model = AZURE_EMBEDDING_MODEL }) => {
})
}

const azureOpenaiImageCreate = async ({ model = AZURE_IMAGE_MODEL, prompt, n = 1, size = '1024x1024', quality = 'standard', style = 'vivid' }) => {
const azureOpenai = new OpenAI({
apiKey: AZURE_OPENAI_KEY,
baseURL: `https://${AZURE_OPENAI_ENDPOINT}.openai.azure.com/openai/deployments/${model}`,
defaultQuery: { 'api-version': AZURE_API_VERSION },
defaultHeaders: { 'api-key': AZURE_OPENAI_KEY },
httpAgent,
timeout: 40000
})
const response = await azureOpenai.images.generate({
model, prompt, n, size, quality, style
})
return JSON.stringify(response.data[0])
}


module.exports = {
openaiChat,
Expand All @@ -164,5 +179,6 @@ module.exports = {
openaiImageCreate,
azureOpenaiChat,
azureOpenaiChatStream,
azureOpenaiEmbedding
azureOpenaiEmbedding,
azureOpenaiImageCreate,
}
25 changes: 17 additions & 8 deletions modules/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const { getQuickJS, shouldInterruptAfterDeadline } = require('quickjs-emscripte
const { shell } = require('electron')
const { js: beautify } = require('js-beautify/js')

let { config: { useProxy, proxyObject, AI_NAME, writeFolder, allowPowerfulInterpreter } } = require('../utils/loadConfig.js')
let { config: { useProxy, proxyObject, AI_NAME, writeFolder, allowPowerfulInterpreter, useAzureOpenai } } = require('../utils/loadConfig.js')
const proxyString = `${proxyObject.protocol}://${proxyObject.host}:${proxyObject.port}`

const { sliceStringbyTokenLength } = require('./tiktoken.js')
const { nodejsInterpreter } = require('./vm.js')
const { openaiImageCreate } = require('./common.js')
const { openaiImageCreate, azureOpenaiImageCreate } = require('./common.js')

let STORE_PATH = path.join(process.cwd(), 'data')
if (!fs.existsSync(STORE_PATH)) {
Expand Down Expand Up @@ -302,12 +302,21 @@ const openLocalFileOrWebpage = async ({ filePath, url, type }) => {
}

const createImageUseDALLE3 = async ({ prompt, size, quality, style }) => {
return await openaiImageCreate({
prompt,
size,
quality,
style
})
if (useAzureOpenai) {
return await azureOpenaiImageCreate({
prompt,
size,
quality,
style
})
} else {
return await openaiImageCreate({
prompt,
size,
quality,
style
})
}
}

module.exports = {
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/components/MessageList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ const openExternalLink = (event) => {
const downloadImage = async (event) => {
event.preventDefault()
let linkElement = document.createElement('a')
let blob = await fetch(event.target.src).then(res => res.blob())
linkElement.setAttribute('download', `${event.target.alt}_${nanoid(6)}.png` || `image_${nanoid(6)}.png`)
linkElement.setAttribute('href', URL.createObjectURL(blob))
linkElement.setAttribute('download', `${event.target.alt}_${nanoid(6)}` || `image_${nanoid(6)}`)
linkElement.setAttribute('href', event.target.src)
linkElement.click()
}
Expand Down
3 changes: 3 additions & 0 deletions src/components/Setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ defineExpose({
<n-form-item label="AZURE_EMBEDDING_MODEL" path="AZURE_EMBEDDING_MODEL" v-show="config.useAzureOpenai">
<n-input v-model:value="config.AZURE_EMBEDDING_MODEL" placeholder="like text-embedding-ada-002" />
</n-form-item>
<n-form-item label="AZURE_IMAGE_MODEL" path="AZURE_IMAGE_MODEL" v-show="config.useAzureOpenai">
<n-input v-model:value="config.AZURE_IMAGE_MODEL" placeholder="like dall-e-3" />
</n-form-item>
<n-form-item label="你的称呼" path="ADMIN_NAME">
<n-input v-model:value="config.ADMIN_NAME" />
</n-form-item>
Expand Down
3 changes: 2 additions & 1 deletion utils/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ try {
AZURE_API_VERSION: '',
AZURE_CHAT_MODEL: '',
AZURE_EMBEDDING_MODEL: '',
AZURE_IMAGE_MODEL: '',
ADMIN_NAME: 'Chell',
AI_NAME: '休留',
systemPrompt: '你是虚拟猫娘休留,以下是你在回复时应该遵循的规则:\n1.你能够灵活的运用猫娘的风格进行回复.\n2.调用函数来提高你的回复质量',
systemPrompt: '你是虚拟猫娘休留,以下是你在回复时应该遵循的规则:\n1.你能够灵活的运用猫娘的风格进行回复.\n2.调用函数来提高你的回复质量.\n3.使用markdown格式回复和显示图片.',
useProxy: false,
proxyObject: {
protocol: 'http',
Expand Down

0 comments on commit 4c0f6fb

Please sign in to comment.