Skip to content

Commit

Permalink
Merge pull request #12 from clairefro/fix-ollama-prompt
Browse files Browse the repository at this point in the history
Create quality parity for Ollama with OpenAI
  • Loading branch information
clairefro committed Mar 5, 2024
2 parents f76af57 + 3d9a20f commit d2403e2
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 42 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,23 @@ While use of OpenAI costs money, it is cheap (as of Nov 2023). Chat sessions wit
To use OpenAI with ChatCBT:

1. [Create](https://auth0.openai.com/u/signup) an OpenAI account
2. Add a payment method to your account, and add a small credit (ex: $10 = roughly 500 ChatCBT sessions!)
2. Add a payment method to your account, and add a small credit (ex: $10 = roughly 500 ChatCBT sessions!)
3. Generate an API Key [here](https://platform.openai.com/api-keys) and copy it to your clipboard
4. Set your OpenAI API Key in ChatCBT plugin settings
6. Ensure "Ollama mode" is **disabled** in ChatCBT plugin settings
5. Ensure "Ollama mode" is **disabled** in ChatCBT plugin settings

Treat your OpenAI API Keys like a password - do not share this publicly. For your safety, your OpenAI API key is encrypted when saving settings.

_**Note:** With the OpenAI option enabled, your messages will be sent to OpenAI. See [OpenAI's data privacy policy](https://openai.com/policies/privacy-policy). To minimize chances of your messages being associated with you personally, I crafted the prompt to respond to a "fictional client" such that it looks like you are creating fake scenarios. As you can guess, this is not foolproof. Try to stick to your emotions, and avoid disclosing any sensitive personal info like real names of people or your home address._

If you prefer to use a different [Ollama model](https://ollama.ai/library), you can specify in the plugin settings.
If you prefer to use a different [Ollama model](https://ollama.ai/library), you can specify in the plugin settings.

### Ollama setup

[Ollama](https://ollama.ai/) is a client that allows you to easily run powerful open source LLM models locally on your machine for free.

Requires Ollama v0.1.24 or higher

**System requirements**

- Available for:
Expand All @@ -84,14 +86,14 @@ If you prefer to use a different [Ollama model](https://ollama.ai/library), you
- Mistral model: 4GB
- At least 8GB of RAM, ideally

1. [download ollama](https://ollama.ai/)
1. [download ollama](https://ollama.ai/) (Get Ollama v0.1.24 or higher)
2. download `mistral` model: in terminal, run `ollama pull mistral`
3. Start local server: in terminal, run `OLLAMA_ORIGINS="*" OLLAMA_HOST="0.0.0.0:11434" ollama serve`
4. Ensure "Ollama mode" is enabled in ChatCBT settings

This will start a local server that hosts your Ollama instance locally on your computer from port `11434`. You can change the port if you like by editing the `OLLAMA_HOST` property in step 3 - just be sure to also update the `Ollama URL` in the ChatCBT plugin settings too. The `OLLAMA_ORIGINS='*'` allows Obsidian to talk to Ollama.

If you prefer to use a different [OpenAI model](https://platform.openai.com/docs/models), you can specify in the plugin settings.
If you prefer to use a different [OpenAI model](https://platform.openai.com/docs/models), you can specify in the plugin settings.

## Usage

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "chat-cbt",
"name": "ChatCBT",
"version": "1.0.3",
"version": "1.1.3",
"minAppVersion": "0.15.0",
"description": "Guides you in reframing negative thoughts and keeping record of your discoveries",
"author": "Claire Froelich",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-chat-cbt-plugin",
"version": "1.0.1",
"version": "1.1.3",
"description": "Plugin to guide you in reframing negative thoughts and keep an organized record of your findings",
"main": "main.js",
"scripts": {
Expand Down
8 changes: 6 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ class MySettingTab extends PluginSettingTab {

new Setting(containerEl)
.setName('OpenAI API Key')
.setDesc('Create an OpenAI API Key from their website and paste here')
.setDesc(
'Create an OpenAI API Key from their website and paste here (Make sure you have added credits to your account!)',
)
.addText((text) =>
text
.setPlaceholder('Enter your API Key')
Expand Down Expand Up @@ -274,7 +276,9 @@ class MySettingTab extends PluginSettingTab {

new Setting(containerEl)
.setName('Ollama server URL')
.setDesc('Edit this if you changed the default port for using Ollama')
.setDesc(
'Edit this if you changed the default port for using Ollama. Requires Ollama v0.1.24 or higher.',
)
.addText((text) =>
text
.setPlaceholder('ex: http://0.0.0.0:11434')
Expand Down
4 changes: 3 additions & 1 deletion src/prompts/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

export default `You are a Cognitive Behavioral Therapist. Your kind and open approach to CBT allows users to confide in you. You will be given an array of dialogue between the therapist and fictional user you refer to in the second person, and your task is to provide a response as the therapist.
You ask questions one by one and collect the user's responses to implement the following steps of CBT:
RESPOND TO THE USER'S LATEST PROMPT, AND KEEP YOUR RESPONSES AS BRIEF AS POSSIBLE.
You ask questions one by one and collect the user's responses to implement the following steps of CBT :
1. Help the user identify troubling situations or conditions in their life.
Expand Down
51 changes: 20 additions & 31 deletions src/util/chatcbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,40 @@ export class ChatCbt {
}

let response = '';
let resolvedModel = model;

const msgs = [SYSTEM_MSG, ...resolvedMsgs];

/** validations should be guaranteed from parent layer, based on mode. Re-validating here to appease typescript gods */
if (mode === 'openai' && !!apiKey) {
const openAiMsgs = [SYSTEM_MSG, ...resolvedMsgs];
response = await this._openai_chat(openAiMsgs, apiKey, model);
const url = 'https://api.openai.com/v1/chat/completions';
response = await this._chat(
url,
msgs,
apiKey,
resolvedModel || 'gpt-3.5-turbo',
);
} else if (mode === 'ollama' && !!ollamaUrl) {
response = await this._ollama_chat(resolvedMsgs, ollamaUrl, model);
const url = ollamaUrl.replace(/\/$/, '') + '/v1/chat/completions';
response = await this._chat(
url,
msgs,
'ollama', // default API Key used by Ollama's OpenAI style chat endpoint (v0.1.24^)
resolvedModel || 'mistral',
);
}

return response;
}

async _openai_chat(
async _chat(
url: string,
messages: Message[],
apiKey: string,
model: string | undefined,
): Promise<string> {
const url = 'https://api.openai.com/v1/chat/completions';

const data = {
model: model || 'gpt-3.5-turbo',
model,
messages,
temperature: 0.7,
};
Expand All @@ -81,28 +94,4 @@ export class ChatCbt {

return response.json.choices[0].message.content;
}

async _ollama_chat(
messages: Message[],
ollamaUrl: string,
model: string | undefined,
): Promise<string> {
const url = ollamaUrl.replace(/\/$/, '') + '/api/generate';

const data = {
model: model || 'mistral',
prompt: JSON.stringify(messages),
system: JSON.stringify(SYSTEM_MSG.content),
stream: false,
};

const options = {
url,
method: 'POST',
body: JSON.stringify(data),
};

const response: { json: { response: string } } = await requestUrl(options);
return response.json.response;
}
}
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"1.0.3": "0.15.0"
"1.1.3": "0.15.0"
}

0 comments on commit d2403e2

Please sign in to comment.