Skip to content

Commit

Permalink
use system prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
chan4lk committed Oct 27, 2023
1 parent 7c6e5c9 commit 92e0d85
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
7 changes: 5 additions & 2 deletions src/services/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ export class OpenAiApi {
});
}
// Asynchronous function to generate text from the OpenAI API
async generateText(prompt, model = "gpt-3.5-turbo", max_tokens = 500, temperature = 0.85) {
async generateText(systemPrompt, prompt, model = "gpt-3.5-turbo", max_tokens = 500, temperature = 0.85) {
// Send a request to the OpenAI API to generate text
try {
const response = await this.openAI.chat.completions.create({
model,
messages: [{ role: "user", content: prompt }],
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: prompt },
],
max_tokens,
n: 1,
temperature,
Expand Down
46 changes: 22 additions & 24 deletions src/taskpane/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export interface AppState {
showModalEdit: boolean;
showModalAdd: boolean;
showPopup: boolean;
promtPreset: any[];
promptPreset: any[];
selectedIndex: number;
promptFromat: string;
promptFormat: string;
}

export default class App extends React.Component<AppProps, AppState> {
Expand All @@ -35,11 +35,11 @@ export default class App extends React.Component<AppProps, AppState> {
listItems: [],
apiKey: "",
presetFormat: "",
promptFromat: "",
promptFormat: "",
showModalEdit: false,
showModalAdd: false,
showPopup: false,
promtPreset: [],
promptPreset: [],
selectedIndex: 0,
};
}
Expand All @@ -53,13 +53,13 @@ export default class App extends React.Component<AppProps, AppState> {
const result = Office.context.document.settings.get("promtPreset");
{
const promtPreset = JSON.parse(result) || [];
this.setState({ promtPreset });
this.setState({ promptPreset: promtPreset });
}
}


toggleModalEdit = () => {
if (this.state.promtPreset.length > 0) {
if (this.state.promptPreset.length > 0) {
if (this.state.selectedIndex === 0) {
this.populateEditFields(0);
}
Expand All @@ -71,7 +71,7 @@ export default class App extends React.Component<AppProps, AppState> {

toggleModalAdd = () => {
this.setState({
promptFromat: 'convert this text'
promptFormat: 'Act as a experienced solutions arechitect. Convert following text into professional text'
});
this.setState((prevState) => ({
showModalAdd: !prevState.showModalAdd,
Expand All @@ -98,17 +98,17 @@ export default class App extends React.Component<AppProps, AppState> {
// Create a new item to add to promtPreset
const newItem = {
format: this.state.presetFormat,
promptFromat: this.state.promptFromat,
promptFromat: this.state.promptFormat,
};
// Add the new item to the existing promtPreset
const updatedPromtPreset = [...this.state.promtPreset, newItem];
const updatedPromtPreset = [...this.state.promptPreset, newItem];

// Save the updated promtPreset settings after converting to JSON
Office.context.document.settings.set("promtPreset", JSON.stringify(updatedPromtPreset));
Office.context.document.settings.saveAsync((result) => {
if (result.status === Office.AsyncResultStatus.Succeeded) {
console.log("Custom setting saved successfully");
this.setState({ promtPreset: updatedPromtPreset }); // Update state with the new promtPreset
this.setState({ promptPreset: updatedPromtPreset }); // Update state with the new promtPreset
} else {
console.error("Error saving custom setting: " + result.error.message);
}
Expand All @@ -118,16 +118,16 @@ export default class App extends React.Component<AppProps, AppState> {

// Function to populate the editing modal fields with the values of the selected item
populateEditFields = (index) => {
const selectedPreset = this.state.promtPreset[index];
const selectedPreset = this.state.promptPreset[index];
if (selectedPreset != "" && selectedPreset != "") {
this.setState({
selectedIndex: index,
presetFormat: selectedPreset.format,
promptFromat: selectedPreset.promptFromat,
promptFormat: selectedPreset.promptFromat,
// Set other modal fields if needed
});
}
console.log("Custom setting" + this.state.promptFromat);
console.log("Custom setting" + this.state.promptFormat);
};
onChangeDropdown = (value, index) => {
this.setState({ presetFormat: value });
Expand All @@ -144,18 +144,18 @@ export default class App extends React.Component<AppProps, AppState> {

const updatedItem = {
format: this.state.presetFormat,
promptFromat: this.state.promptFromat,
promptFromat: this.state.promptFormat,
};

const updatedPromtPreset = [...this.state.promtPreset];
const updatedPromtPreset = [...this.state.promptPreset];
updatedPromtPreset[selectedIndex] = updatedItem;

Office.context.document.settings.set("promtPreset", JSON.stringify(updatedPromtPreset));
Office.context.document.settings.saveAsync((result) => {
if (result.status === Office.AsyncResultStatus.Succeeded) {
console.log("Custom setting saved successfully");
this.setState({
promtPreset: updatedPromtPreset,
promptPreset: updatedPromtPreset,
selectedIndex: -1,
});
} else {
Expand All @@ -176,11 +176,9 @@ export default class App extends React.Component<AppProps, AppState> {
const word = words.load();
await context.sync();
const openai = new OpenAiApi(this.state.apiKey);
const prompt = `${this.state.promptFromat}', ${word.text.trim()}.`;

const converted = await openai.generateText(prompt);
const converted = await openai.generateText(this.state.promptFormat, word.text.trim());
console.log(converted);
console.log(prompt.toString());
words.insertText(converted, "Replace");

await context.sync();
Expand Down Expand Up @@ -230,7 +228,7 @@ export default class App extends React.Component<AppProps, AppState> {
className="ms-dropbtn"
onChange={(e) => this.onChangeDropdown(e.target.value, e.target.selectedIndex)}
>
{this.state.promtPreset.map((preset, index) => (
{this.state.promptPreset.map((preset, index) => (
<option key={index} value={preset.format}>
{preset.format}
</option>
Expand Down Expand Up @@ -268,11 +266,11 @@ export default class App extends React.Component<AppProps, AppState> {
<textarea
id="noter-text-area"
name="textarea"
value={this.state.promptFromat}
value={this.state.promptFormat}
className="ms-input-3-1"
title="Your Prompt"
style={{ fontSize: "15.rem", width: "243px", height: "243px" }}
onChange={(e) => this.setState({ promptFromat: e.target.value })}
onChange={(e) => this.setState({ promptFormat: e.target.value })}
></textarea>
</div>
</div>
Expand Down Expand Up @@ -314,8 +312,8 @@ export default class App extends React.Component<AppProps, AppState> {
className="ms-input-3-1"
title="Your Prompt"
style={{ fontSize: "15.rem", width: "243px", height: "243px" }}
value={this.state.promptFromat}
onChange={(e) => this.setState({ promptFromat: e.target.value })}
value={this.state.promptFormat}
onChange={(e) => this.setState({ promptFormat: e.target.value })}
></textarea>
</div>
</div>
Expand Down

0 comments on commit 92e0d85

Please sign in to comment.