Skip to content

Commit

Permalink
Merge pull request #1354 from mito-ds/ai-taskpane-msg-edit
Browse files Browse the repository at this point in the history
AI Taskpane - Added Message Edit
  • Loading branch information
aarondr77 authored Nov 7, 2024
2 parents e314d3c + 1f368a6 commit 9057a40
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 280 deletions.
97 changes: 10 additions & 87 deletions mito-ai/Untitled.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,19 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "28ee61af-b8be-42d0-b973-70c6a1f45005",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e45a2574-6720-416b-af1a-f1125397e77f",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "unterminated string literal (detected at line 1) (1814187833.py, line 1)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[1], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m print('hello world\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unterminated string literal (detected at line 1)\n"
]
}
],
"source": [
"print('hello world')"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e8a0d14d-a615-403e-b993-743f9c1ce89d",
"metadata": {},
"outputs": [],
"source": [
"a = 0 # Define a with an initial value\n",
"b = 5 # Define b with an initial value\n",
"y = 1\n",
"z = b + y"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "04aa0b2e-6066-405d-8aa4-6c605a1c3ec3",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"# Creating sample data and columns for the new dataframe\n",
"data = {\n",
" 'Name': ['Alice', 'Bob', 'Charlie'],\n",
" 'Age': [25, 30, 35],\n",
" 'City': ['New York', 'Los Angeles', 'Chicago']\n",
"}\n",
"columns = ['Name', 'Age', 'City']\n",
"\n",
"# Creating the new dataframe\n",
"df = pd.DataFrame(data, columns=columns)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89bc8863-155c-41fd-bba0-e76ff9e9226b",
"metadata": {},
"outputs": [],
"source": [
"AARON"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd6942a3-f631-4bab-b405-8aaa4e5504d9",
"execution_count": 22,
"id": "c0a673c8-32b2-4aff-821b-9b989903f975",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"data = {\n",
" 'Column1': [],\n",
" 'Column2': []\n",
" 'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],\n",
" 'age': [25, 30, 35, 40, 28],\n",
" 'city': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],\n",
" 'occupation': ['Engineer', 'Doctor', 'Artist', 'Lawyer', 'Nurse'],\n",
" 'salary': [70000, 120000, 50000, 100000, 60000]\n",
"}\n",
"\n",
"df = pd.DataFrame(data)"
Expand All @@ -91,19 +23,10 @@
{
"cell_type": "code",
"execution_count": null,
"id": "17ca9b56-07d1-4a5a-ba85-96e1bc2dc9d1",
"id": "8b7e34f2-42ee-4430-8216-4314485262d9",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"data = {\n",
" 'Column1': [],\n",
" 'Column2': []\n",
"}\n",
"\n",
"df = pd.DataFrame(data)"
]
"source": []
}
],
"metadata": {
Expand All @@ -122,7 +45,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.11.1"
}
},
"nbformat": 4,
Expand Down
31 changes: 29 additions & 2 deletions mito-ai/Untitled12.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
{
"cells": [],
"metadata": {},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "40a1e2c4-07ff-4ea4-82d7-c225a5a5f013",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
155 changes: 21 additions & 134 deletions mito-ai/src/Extensions/AiChat/ChatHistoryManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import OpenAI from "openai";
import { IVariableManager } from "../VariableManager/VariableManagerPlugin";
import { INotebookTracker } from '@jupyterlab/notebook';
import { getActiveCellCode } from "../../utils/notebook";

import { createBasicPrompt, createErrorPrompt, createExplainCodePrompt } from "./PromptManager";
export interface IDisplayOptimizedChatHistory {
message: OpenAI.Chat.ChatCompletionMessageParam
type: 'openai message' | 'connection error'
Expand Down Expand Up @@ -79,126 +79,37 @@ export class ChatHistoryManager {

const aiOptimizedMessage: OpenAI.Chat.ChatCompletionMessageParam = {
role: 'user',
content: `You have access to the following variables:
${variables?.map(variable => `${JSON.stringify(variable, null, 2)}\n`).join('')}
Complete the task below. Decide what variables to use and what changes you need to make to the active code cell. Only return the full new active code cell and a concise explanation of the changes you made.
<Reminders>
Do not:
- Use the word "I"
- Include multiple approaches in your response
- Recreate variables that already exist
Do:
- Use the variables that you have access to
- Keep as much of the original code as possible
- Ask for more context if you need it.
</Reminders>
<Example>
Code in the active code cell:
\`\`\`python
import pandas as pd
loans_df = pd.read_csv('./loans.csv')
\`\`\`
Your task: convert the issue_date column to datetime.
Output:
\`\`\`python
import pandas as pd
loans_df = pd.read_csv('./loans.csv')
loans_df['issue_date'] = pd.to_datetime(loans_df['issue_date'])
\`\`\`
Use the pd.to_datetime function to convert the issue_date column to datetime.
</Example>
Code in the active code cell:
\`\`\`python
${activeCellCode}
\`\`\`
Your task: ${input}`};
content: createBasicPrompt(variables, activeCellCode || '', input)
};

this.history.displayOptimizedChatHistory.push({message: getDisplayedOptimizedUserMessage(input, activeCellCode), type: 'openai message'});
this.history.aiOptimizedChatHistory.push(aiOptimizedMessage);
}

addDebugErrorMessage(errorMessage: string): void {

updateMessageAtIndex(index: number, newContent: string): void {
const variables = this.variableManager.variables
const activeCellCode = getActiveCellCode(this.notebookTracker)

const aiOptimizedPrompt = `You just ran the active code cell and received an error. Return the full code cell with the error corrected and a short explanation of the error.
<Reminders>
Do not:
- Use the word "I"
- Include multiple approaches in your response
- Recreate variables that already exist
Do:
- Use the variables that you have access to
- Keep as much of the original code as possible
- Ask for more context if you need it.
</Reminders>
<Important Jupyter Context>
Remember that you are executing code inside a Jupyter notebook. That means you will have persistent state issues where variables from previous cells or previous code executions might still affect current code. When those errors occur, here are a few possible solutions:
1. Restarting the kernel to reset the environment if a function or variable has been unintentionally overwritten.
2. Identify which cell might need to be rerun to properly initialize the function or variable that is causing the issue.
For example, if an error occurs because the built-in function 'print' is overwritten by an integer, you should return the code cell with the modification to the print function removed and also return an explanation that tell the user to restart their kernel. Do not add new comments to the code cell, just return the code cell with the modification removed.
When a user hits an error because of a persistent state issue, tell them how to resolve it.
</Important Jupyter Context>
<Example>
Code in the active code cell:
\`\`\`python
print(y)
\`\`\`
Error Message:
NameError: name 'y' is not defined
Output:
\`\`\`python
y = 10
print(y)
\`\`\`
const aiOptimizedMessage: OpenAI.Chat.ChatCompletionMessageParam = {
role: 'user',
content: createBasicPrompt(variables, activeCellCode || '', newContent)
};

The variable y has not yet been created.Define the variable y before printing it.
</Example>
Code in the active code cell:
// Update the message at the specified index
this.history.aiOptimizedChatHistory[index] = aiOptimizedMessage;
this.history.displayOptimizedChatHistory[index].message = getDisplayedOptimizedUserMessage(newContent, activeCellCode);

\`\`\`python
${activeCellCode}
\`\`\`
// Remove all messages after the index we're updating
this.history.aiOptimizedChatHistory = this.history.aiOptimizedChatHistory.slice(0, index + 1);
this.history.displayOptimizedChatHistory = this.history.displayOptimizedChatHistory.slice(0, index + 1);
}

Error Message:
addDebugErrorMessage(errorMessage: string): void {

const activeCellCode = getActiveCellCode(this.notebookTracker)

${errorMessage}
const aiOptimizedPrompt = createErrorPrompt(errorMessage, activeCellCode || '')

Output:
`

this.history.displayOptimizedChatHistory.push({message: getDisplayedOptimizedUserMessage(errorMessage, activeCellCode), type: 'openai message'});
this.history.aiOptimizedChatHistory.push({role: 'user', content: aiOptimizedPrompt});
Expand All @@ -207,31 +118,7 @@ Output:
addExplainCodeMessage(): void {
const activeCellCode = getActiveCellCode(this.notebookTracker)

const aiOptimizedPrompt = `Explain the code in the active code cell to me like I have a basic understanding of Python. Don't explain each line, but instead explain the overall logic of the code.
<Example>
Code in the active code cell:
\`\`\`python
def multiply(x, y):
return x * y
\`\`\`
Output:
This code creates a function called \`multiply\` that takes two arguments \`x\` and \`y\`, and returns the product of \`x\` and \`y\`.
</Example>
Code in the active code cell:
\`\`\`python
${activeCellCode}
\`\`\`
Output:
`
const aiOptimizedPrompt = createExplainCodePrompt(activeCellCode || '')

this.history.displayOptimizedChatHistory.push({message: getDisplayedOptimizedUserMessage('Explain this code', activeCellCode), type: 'openai message'});
this.history.aiOptimizedChatHistory.push({role: 'user', content: aiOptimizedPrompt});
Expand Down
Loading

0 comments on commit 9057a40

Please sign in to comment.