Skip to content

Commit

Permalink
Merge pull request #13 from InterwebAlchemy/feature/prep-for-release
Browse files Browse the repository at this point in the history
Prep for Obsidian Community Plugin Release
  • Loading branch information
ericrallen authored Aug 10, 2023
2 parents 3a1aac1 + b090ffc commit 261990a
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 93 deletions.
2 changes: 1 addition & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: { 'body-max-line-length': [0] },
rules: { 'body-max-line-length': [0] }
}
25 changes: 11 additions & 14 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
},
"dependencies": {
"@agney/react-loading": "^0.1.2",
"gpt3-tokenizer": "^1.1.5",
"gpt-tokenizer": "^2.1.1",
"openai": "^3.2.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
19 changes: 14 additions & 5 deletions src/components/ConversationSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ const ConversationSettings = ({
const [userHandle, setUserHandle] = useState('')
const [botHandle, setBotHandle] = useState('')
const [maxTokens, setMaxTokens] = useState(0)
const [temperature, setTemperature] = useState(0)
const [temperature, setTemperature] = useState(
`${OPEN_AI_DEFAULT_TEMPERATURE}`
)

const changeTemperature = (temperatureString: string): void => {
setTemperature(Number(temperatureString))
setTemperature(temperatureString)
}

const changeMaxTokens = (maxTokensString: string): void => {
Expand All @@ -42,7 +44,7 @@ const ConversationSettings = ({
useEffect(() => {
if (typeof conversation !== 'undefined' && conversation !== null) {
setTemperature(
conversation.settings.temperature ?? OPEN_AI_DEFAULT_TEMPERATURE
`${conversation.settings.temperature ?? OPEN_AI_DEFAULT_TEMPERATURE}`
)
}
}, [conversation])
Expand Down Expand Up @@ -81,9 +83,16 @@ const ConversationSettings = ({
if (
typeof conversation !== 'undefined' &&
conversation !== null &&
temperature !== conversation?.settings.temperature
Number(temperature) !== conversation?.settings.temperature
) {
conversation.settings.temperature = temperature
const temp = Number(temperature)

// make sure temperature is valid value
if (temp >= 0 && temp <= 1) {
conversation.settings.temperature = temp
} else {
conversation.settings.temperature = OPEN_AI_DEFAULT_TEMPERATURE
}
}
}, [temperature])

Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const USER_MESSAGE_OBJECT_TYPE = `${PLUGIN_PREFIX.replace(
)}_user_message`
export const USER_HANDLE = 'You:'
export const BOT_HANDLE = 'ChatGPT:'
export const DEFAULT_TOKEN_TYPE = 'gpt3'
export const DEFAULT_TOKEN_TYPE = 'gpt4'
export const DEFAULT_MAX_MEMORY_COUNT = 10
export const DEFAULT_MAX_TOKENS = DEFAULT_MODEL.maxTokens
export const DEFAULT_TOKEN_BUFFER = Math.floor(DEFAULT_MAX_TOKENS / 4)
Expand Down
29 changes: 17 additions & 12 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { type App, Plugin, type PluginManifest, TFile, Notice } from 'obsidian'
import {
type App,
Plugin,
type PluginManifest,
TFile,
Notice,
normalizePath
} from 'obsidian'

import ChatView from './views/ChatView'
import SettingsTab from './views/SettingsTab'
Expand Down Expand Up @@ -88,7 +95,7 @@ export default class ObsidianAIResearchAssistant extends Plugin {
initializeLogger(): void {
this.logger = new Logger({ settings: this.settings })

this.logger.debug('Logger initialized...')
this.logger.debug(`${PLUGIN_NAME} Logger initialized...`)
}

async onload(): Promise<void> {
Expand All @@ -106,10 +113,6 @@ export default class ObsidianAIResearchAssistant extends Plugin {
await this.initializeChatInterface()
}

onunload(): void {
this.app.workspace.detachLeavesOfType(PLUGIN_PREFIX)
}

async loadSettings(): Promise<void> {
this.settings = Object.assign({}, PLUGIN_SETTINGS, await this.loadData())
}
Expand All @@ -121,7 +124,9 @@ export default class ObsidianAIResearchAssistant extends Plugin {
async checkForExistingFile(title: string): Promise<boolean> {
const filePath = this.settings.conversationHistoryDirectory

const file = `${filePath}/${title.replace(/[\\:/]/g, '_')}.md`
const file = normalizePath(
`${filePath}/${title.replace(/[\\:/]/g, '_')}.md`
)

const existingFile = this.app.vault.getAbstractFileByPath(file)

Expand All @@ -131,7 +136,8 @@ export default class ObsidianAIResearchAssistant extends Plugin {
async saveConversation(conversation: Conversation): Promise<void> {
let canSave = true

const filePath = this.settings.conversationHistoryDirectory
// NOTE: we normalize the path when we save the settings, so we may not need to normalize it here as well
const filePath = normalizePath(this.settings.conversationHistoryDirectory)

const existingDirectory = this.app.vault.getAbstractFileByPath(filePath)

Expand All @@ -147,10 +153,9 @@ export default class ObsidianAIResearchAssistant extends Plugin {
}

if (canSave) {
const file = `${filePath}/${conversation.title.replace(
/[\\:/]/g,
'_'
)}.md`
const file = normalizePath(
`${filePath}/${conversation.title.replace(/[\\:/]/g, '_')}.md`
)

const existingFile = this.app.vault.getAbstractFileByPath(file)

Expand Down
23 changes: 4 additions & 19 deletions src/services/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const openAICompletion = async (
settings: PluginSettings = PLUGIN_SETTINGS,
logger: Logger
): Promise<OpenAICompletion | CreateChatCompletionResponse> => {
const { userHandle, botHandle, debugMode, openAiApiKey } = settings
const { userHandle, botHandle, openAiApiKey } = settings

let apiKey = openAiApiKey

Expand All @@ -64,24 +64,17 @@ export const openAICompletion = async (

const messages = formatChat(input as Conversation)

console.log(messages)

logger.log('messages', messages)

const completion = await openai.createChatCompletion({
model: model.model,
messages
})

console.log(completion)

return completion.data
} catch (error) {
if (typeof error?.response !== 'undefined') {
console.log(error.response.status)
console.log(error.response.data)
logger.error(error.response.status, error.response.data)
} else {
console.log(error.message)
logger.error(error.message)
}

throw error
Expand Down Expand Up @@ -132,21 +125,13 @@ export const openAICompletion = async (
throw: false
}

if (debugMode) {
console.debug('REQUEST:', request)
}

try {
const response = await obsidianRequest(request)

if (debugMode) {
console.debug('RESPONSE:', response)
}

if (response.status < 400) {
return response.json
} else {
console.error(response)
logger.error(response)

throw new Error(response.text)
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/openai/models/gpt-3.5-turbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const GPT35Turbo: ModelDefinition = {
adapter: GPT35TurboAdapter,
model: 'gpt-3.5-turbo',
maxTokens: 4000,
tokenType: 'gpt3'
tokenType: 'gpt4'
}

export default GPT35Turbo
26 changes: 11 additions & 15 deletions src/utils/tokenCounter.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import GPT3Tokenizer from 'gpt3-tokenizer'
import { encode } from 'gpt-tokenizer'

import { encode as oldEncoder } from 'gpt-tokenizer/esm/model/text-davinci-003'

import { DEFAULT_TOKEN_TYPE } from '../constants'

export type TokenCounterType = 'gpt3' | 'codex'
export type TokenCounterType = 'gpt3' | 'gpt4'

export interface TokenCounterOptions {
// TODO: figure out what may need to be done to support other adapters and models
type?: TokenCounterType
debug?: boolean
prefix?: string
}

const tokenCounter = (text: string, options: TokenCounterOptions = {}): number => {
const { type = DEFAULT_TOKEN_TYPE, debug = false } = options

const tokenizer = new GPT3Tokenizer({ type })

const tokens = tokenizer.encode(text)
const tokenCounter = (
text: string,
options: TokenCounterOptions = {}
): number => {
const { type = DEFAULT_TOKEN_TYPE } = options

if (debug) {
console.debug('TOKENS:', tokens)
}
const tokens = type === 'gpt3' ? oldEncoder(text) : encode(text)

return tokens.text.length
return tokens.length
}

export default tokenCounter
2 changes: 1 addition & 1 deletion src/views/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default class ChatView extends ItemView {

this.debugButton = new ButtonComponent(toolbar)
this.debugButton.setButtonText('Debug')
this.debugButton.setTooltip('Log Conversation to this.plugin.logger')
this.debugButton.setTooltip('Log Conversation to DevTools')
this.debugButton.setIcon('curly-braces')

if (
Expand Down
Loading

0 comments on commit 261990a

Please sign in to comment.