Skip to content

Commit

Permalink
[CHATGPT] Feature & Hotfix 5/18/23 (#6643)
Browse files Browse the repository at this point in the history
* refactor: `ConfigurationPreferences`

* feat: `apiEndpoint`

* fix: remove default initial check for `isHistoryPaused`

* fix: copy

* fix: `404` error code when `useAzure` enabled

* fix: better error handle for selected text

* docs: changelog
  • Loading branch information
abielzulio authored May 19, 2023
1 parent 5fd1b0e commit c305759
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 43 deletions.
5 changes: 5 additions & 0 deletions extensions/chatgpt/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [Fix & Feature] - May, 18 2023

- Feature: Custom API Endpoint
- Fix: 404 error code while Use Azure is enabled

## [Fix & Feature] - May, 2023

- Feature: Azure OpenAI ([@sykuang](https://github.com/abielzulio/chatgpt-raycast/pull/36))
Expand Down
35 changes: 18 additions & 17 deletions extensions/chatgpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,24 @@ This extension requires a valid `Secret Key` as your API Key from [OpenAI](https

All preferences properties list that can be customize through `Raycast Settings > Extensions > ChatGPT`

| Properties | Label | Value | Required | Default | Description |
| ------------------------ | ---------------------- | ----------------------------------- | -------- | ------- | ---------------------------------------------------------------------------------------------------------------- |
| `apiKey` | API Key | `string` | `true` | `empty` | Your personal OpenAI API key |
| `useStream` | Stream Completion | `boolean` | `true` | `true` | Stream the completions of the generated answer |
| `isAutoSaveConversation` | Auto-save Conversation | `boolean` | `true` | `true` | Auto-save every conversation that you had with the model |
| `isHistoryPaused` | Pause History | `boolean` | `false` | `false` | Pause the history of the conversation |
| `isAutoLoadText` | Auto-load | `boolean` | `false` | `false` | Load selected text from your frontmost application to the `question bar` or `full text input form` automatically |
| `isAutoFullInput` | Use Full Text Input | `boolean` | `false` | `false` | Switch to `full text input form` from `question bar` automatically whenever you want to ask or type a question |
| `isAutoTTS` | Text-to-Speech | `boolean` | `false` | `false` | Enable auto text-to-speech everytime you get a generated answer |
| `useProxy` | Use Proxy | `boolean` | `false` | `false` | Each question request will be passed through the proxy |
| `proxyProtocol` | Proxy Protocol | `http`, `https`, `socks4`, `socks5` | `false` | `http` | Proxy protocol option |
| `proxyHost` | Proxy Host | `string` | `false` | `empty` | Proxy host value |
| `proxyUsername` | Proxy Username | `string` | `false` | `empty` | Proxy username value |
| `proxyPassword` | Proxy Password | `string` | `false` | `empty` | Proxy password value |
| `useAzure` | Use Azure OpenAI | `boolean` | `true` | `false` | Use Azure OPENAI rather than OPENAI |
| `azureEndpoint` | Azure Endpoint | `string` | `false` | `empty` | Azure OpenAI resource endpoint |
| `azureDeploymentName` | Azure Deployment | `string` | `false` | `empty` | Azure OpenAI resource deployment |
| Properties | Label | Value | Required | Default | Description |
| ------------------------ | ---------------------- | ----------------------------------- | -------- | --------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| `apiKey` | API Key | `string` | `true` | `empty` | Your personal OpenAI API key |
| `useStream` | Stream Completion | `boolean` | `true` | `true` | Stream the completions of the generated answer |
| `isAutoSaveConversation` | Auto-save Conversation | `boolean` | `true` | `true` | Auto-save every conversation that you had with the model |
| `isHistoryPaused` | Pause History | `boolean` | `false` | `false` | Pause the history of the conversation |
| `isAutoLoadText` | Auto-load | `boolean` | `false` | `false` | Load selected text from your frontmost application to the `question bar` or `full text input form` automatically |
| `isAutoFullInput` | Use Full Text Input | `boolean` | `false` | `false` | Switch to `full text input form` from `question bar` automatically whenever you want to ask or type a question |
| `isAutoTTS` | Text-to-Speech | `boolean` | `false` | `false` | Enable auto text-to-speech everytime you get a generated answer |
| `apiEndpoint` | API Endpoint | `string` | `false` | `https://api.openai.com/v1` | Change the OpenAI's default API endpoint to custom endpoint |
| `useProxy` | Use Proxy | `boolean` | `false` | `false` | Each question request will be passed through the proxy |
| `proxyProtocol` | Proxy Protocol | `http`, `https`, `socks4`, `socks5` | `false` | `http` | Proxy protocol option |
| `proxyHost` | Proxy Host | `string` | `false` | `empty` | Proxy host value |
| `proxyUsername` | Proxy Username | `string` | `false` | `empty` | Proxy username value |
| `proxyPassword` | Proxy Password | `string` | `false` | `empty` | Proxy password value |
| `useAzure` | Use Azure OpenAI | `boolean` | `true` | `false` | Use Azure OPENAI rather than OPENAI |
| `azureEndpoint` | Azure Endpoint | `string` | `false` | `empty` | Azure OpenAI resource endpoint |
| `azureDeploymentName` | Azure Deployment | `string` | `false` | `empty` | Azure OpenAI resource deployment |

### How to use Azure OpenAI

Expand Down
13 changes: 11 additions & 2 deletions extensions/chatgpt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"description": "Pause the history of the conversation",
"type": "checkbox",
"title": "Pause History",
"default": true,
"default": false,
"required": false
},
{
Expand Down Expand Up @@ -125,6 +125,15 @@
"required": false,
"default": false
},
{
"name": "apiEndpoint",
"label": "Change API Endpoint",
"description": "Change the OpenAI's default API endpoint to custom endpoint",
"type": "checkbox",
"title": "API Endpoint",
"placeholder": "https://api.openai.com/v1",
"required": false
},
{
"name": "useProxy",
"label": "Enable proxy for each request",
Expand Down Expand Up @@ -162,7 +171,7 @@
},
{
"name": "proxyHost",
"description": "Sever address of the proxy",
"description": "Server address of the proxy",
"type": "textfield",
"title": "Proxy Host",
"required": false
Expand Down
27 changes: 10 additions & 17 deletions extensions/chatgpt/src/hooks/useChatGPT.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import { ConfigurationPreferences } from "../type";

export function useChatGPT(): OpenAIApi {
const [chatGPT] = useState(() => {
const preferences = getPreferenceValues<{
apiKey: string;
useAzure: boolean;
azureEndpoint: string;
azureDeployment: string;
}>();
const getConfig = function ({ useAzure, apiKey, azureEndpoint, azureDeployment }: ConfigurationPreferences) {
if (useAzure) {
const preferences = getPreferenceValues<ConfigurationPreferences>();
const getConfig = function (params: ConfigurationPreferences) {
if (params.useAzure) {
return new Configuration({
apiKey,
basePath: azureEndpoint + "/openai/deployments/" + azureDeployment,
apiKey: params.apiKey,
basePath: params.azureEndpoint + "/openai/deployments/" + params.azureDeployment,
});
} else {
return new Configuration({ apiKey });
return new Configuration({
apiKey: params.apiKey,
basePath: params.apiEndpoint ?? "https://api.openai.com/v1",
});
}
};
const config = getConfig(preferences);
Expand All @@ -28,10 +26,5 @@ export function useChatGPT(): OpenAIApi {
}

export function getConfiguration(): ConfigurationPreferences {
return getPreferenceValues<{
apiKey: string;
useAzure: boolean;
azureEndpoint: string;
azureDeployment: string;
}>();
return getPreferenceValues<ConfigurationPreferences>();
}
15 changes: 8 additions & 7 deletions extensions/chatgpt/src/hooks/useModel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LocalStorage, showToast, Toast } from "@raycast/api";
import { useCallback, useEffect, useMemo, useState } from "react";
import { Model, ModelHook } from "../type";
import { useChatGPT } from "./useChatGPT";
import { getConfiguration, useChatGPT } from "./useChatGPT";

export const DEFAULT_MODEL: Model = {
id: "default",
Expand All @@ -17,16 +17,17 @@ export const DEFAULT_MODEL: Model = {
export function useModel(): ModelHook {
const [data, setData] = useState<Model[]>([]);
const [isLoading, setLoading] = useState<boolean>(true);

const gpt = useChatGPT();

const { useAzure, azureDeployment } = getConfiguration();
const [option, setOption] = useState<Model["option"][]>(["gpt-3.5-turbo", "gpt-3.5-turbo-0301"]);

useEffect(() => {
gpt.listModels().then((res) => {
const models = res.data.data;
setOption(models.filter((m) => m.id.startsWith("gpt")).map((x) => x.id));
});
if (!useAzure) {
gpt.listModels().then((res) => {
const models = res.data.data;
setOption(models.filter((m) => m.id.startsWith("gpt")).map((x) => x.id));
});
}
}, [gpt]);

useEffect(() => {
Expand Down
5 changes: 5 additions & 0 deletions extensions/chatgpt/src/hooks/useQuestion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export function useQuestion(props: { initialQuestion: string; disableAutoLoad?:
style: Toast.Style.Success,
title: "Selected text loaded!",
});
} else {
await showToast({
style: Toast.Style.Success,
title: "No text selected!",
});
}
} catch (error) {
await showToast({
Expand Down
1 change: 1 addition & 0 deletions extensions/chatgpt/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,5 @@ export interface ConfigurationPreferences {
useAzure: boolean;
azureEndpoint: string;
azureDeployment: string;
apiEndpoint: string;
}

0 comments on commit c305759

Please sign in to comment.