Skip to content

Commit

Permalink
🐛 Unable to set up Azure OpenAI #10469
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Feb 28, 2024
1 parent 9f69d7d commit 77885a8
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/appearance/langs/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@
"apiBaseURL": "API Base URL",
"apiBaseURLTip": "The base address of the request, such as <code class='fn__code'>https://api.openai.com/v1</code>",
"apiUserAgentTip": "The user agent that initiated the request, that is, the HTTP header <code class='fn__code'>User-Agent</code>",
"apiVersion": "API version",
"apiVersionTip": "Only required when using the Azure OpenAI service",
"apiProvider": "API Provider",
"apiProviderTip": "Use the selected API provider to implement AI-related functions",
"skip": "Skip",
Expand Down
2 changes: 2 additions & 0 deletions app/appearance/langs/es_ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@
"apiBaseURL": "URL base de la API",
"apiBaseURLTip": "La dirección base de la solicitud, como <code class='fn__code'>https://api.openai.com/v1</code>",
"apiUserAgentTip": "El agente de usuario que inició la solicitud, es decir, el encabezado HTTP <code class='fn__code'>User-Agent</code>",
"apiVersion": "Versión API",
"apiVersionTip": "Sólo es necesario cuando se utiliza el servicio Azure OpenAI",
"apiProvider": "API proveedor",
"apiProviderTip": "Utilice el proveedor de API seleccionado para implementar funciones relacionadas con la IA",
"skip": "barco",
Expand Down
2 changes: 2 additions & 0 deletions app/appearance/langs/fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@
"apiBaseURL": "URL de base de l'API",
"apiBaseURLTip": "L'adresse de base de la requête, telle que <code class='fn__code'>https://api.openai.com/v1</code>",
"apiUserAgentTip": "L'agent utilisateur qui a initié la requête, c'est-à-dire l'en-tête HTTP <code class='fn__code'>User-Agent</code>",
"apiVersion": "Version API",
"apiVersionTip": "Requis uniquement lors de l'utilisation du service Azure OpenAI",
"apiProvider": "API du fournisseur",
"apiProviderTip": "Utiliser le fournisseur d'API sélectionné pour implémenter les fonctions liées à l'IA",
"skip": "Navire",
Expand Down
2 changes: 2 additions & 0 deletions app/appearance/langs/zh_CHT.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@
"apiBaseURL": "API 基礎地址",
"apiBaseURLTip": "發起請求的基礎地址,如 <code class='fn__code'>https://api.openai.com/v1</code>",
"apiUserAgentTip": "發起請求的使用者代理,即 HTTP 標頭 <code class='fn__code'>User-Agent</code>",
"apiVersion": "API 版本",
"apiVersionTip": "僅在使用 Azure OpenAI 服務時需要設定",
"apiProvider": "API 提供商",
"apiProviderTip": "選擇後將使用該提供商的 API 服務實現 AI 相關功能",
"skip": "跳過",
Expand Down
2 changes: 2 additions & 0 deletions app/appearance/langs/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@
"apiMaxTokensTip": "请求 API 时传入的 <code class='fn__code'>max_tokens</code> 参数,用于控制生成的文本长度",
"apiBaseURL": "API 基础地址",
"apiBaseURLTip": "发起请求的基础地址,如 <code class='fn__code'>https://api.openai.com/v1</code>",
"apiVersion": "API 版本",
"apiVersionTip": "仅在使用 Azure OpenAI 服务时需要设置",
"apiUserAgentTip": "发起请求的用户代理,即 HTTP 标头 <code class='fn__code'>User-Agent</code>",
"apiProvider": "API 提供商",
"apiProviderTip": "选择后将使用该提供商的 API 服务实现 AI 相关功能",
Expand Down
15 changes: 15 additions & 0 deletions app/src/config/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export const ai = {
<input class="b3-text-field fn__block" id="apiBaseURL" value="${window.siyuan.config.ai.openAI.apiBaseURL}"/>
<div class="b3-label__text">${window.siyuan.languages.apiBaseURLTip}</div>
</div>
<div class="b3-label">
${window.siyuan.languages.apiVersion}
<div class="fn__hr"></div>
<input class="b3-text-field fn__block" id="apiVersion" value="${window.siyuan.config.ai.openAI.apiVersion}"/>
<div class="b3-label__text">${window.siyuan.languages.apiVersionTip}</div>
</div>
<div class="b3-label">
User-Agent
<div class="fn__hr"></div>
Expand Down Expand Up @@ -119,6 +125,14 @@ export const ai = {
<input class="b3-text-field fn__block" id="apiBaseURL" value="${window.siyuan.config.ai.openAI.apiBaseURL}"/>
</div>
</div>
<div class="fn__flex b3-label">
<div class="fn__block">
${window.siyuan.languages.apiVersion}
<div class="b3-label__text">${window.siyuan.languages.apiVersionTip}</div>
<span class="fn__hr"></span>
<input class="b3-text-field fn__block" id="apiVersion" value="${window.siyuan.config.ai.openAI.apiVersion}"/>
</div>
</div>
<div class="fn__flex b3-label">
<div class="fn__block">
User-Agent
Expand Down Expand Up @@ -146,6 +160,7 @@ export const ai = {
openAI: {
apiUserAgent: (ai.element.querySelector("#apiUserAgent") as HTMLInputElement).value,
apiBaseURL: (ai.element.querySelector("#apiBaseURL") as HTMLInputElement).value,
apiVersion: (ai.element.querySelector("#apiVersion") as HTMLInputElement).value,
apiKey: (ai.element.querySelector("#apiKey") as HTMLInputElement).value,
apiModel: (ai.element.querySelector("#apiModel") as HTMLSelectElement).value,
apiMaxTokens: parseInt((ai.element.querySelector("#apiMaxTokens") as HTMLInputElement).value),
Expand Down
3 changes: 2 additions & 1 deletion app/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ interface Window {
}
mermaid: {
initialize(options: any): void,
render(id: string, text: string): { svg:string }
render(id: string, text: string): { svg: string }
};
plantumlEncoder: {
encode(options: string): string,
Expand Down Expand Up @@ -727,6 +727,7 @@ interface IConfig {
apiProvider: string // OpenAI, Azure
apiUserAgent: string
apiBaseURL: string
apiVersion: string
apiKey: string
apiModel: string
apiMaxTokens: number
Expand Down
1 change: 1 addition & 0 deletions kernel/conf/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type OpenAI struct {
APIBaseURL string `json:"apiBaseURL"`
APIUserAgent string `json:"apiUserAgent"`
APIProvider string `json:"apiProvider"` // OpenAI, Azure
APIVersion string `json:"apiVersion"` // Azure API version
}

func NewAI() *AI {
Expand Down
2 changes: 1 addition & 1 deletion kernel/model/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func chatGPTContinueWrite(msg string, contextMsgs []string, cloud bool) (ret str
if cloud {
gpt = &CloudGPT{}
} else {
gpt = &OpenAIGPT{c: util.NewOpenAIClient(Conf.AI.OpenAI.APIKey, Conf.AI.OpenAI.APIProxy, Conf.AI.OpenAI.APIBaseURL, Conf.AI.OpenAI.APIUserAgent)}
gpt = &OpenAIGPT{c: util.NewOpenAIClient(Conf.AI.OpenAI.APIKey, Conf.AI.OpenAI.APIProxy, Conf.AI.OpenAI.APIBaseURL, Conf.AI.OpenAI.APIUserAgent, Conf.AI.OpenAI.APIVersion, Conf.AI.OpenAI.APIProvider)}
}

buf := &bytes.Buffer{}
Expand Down
7 changes: 6 additions & 1 deletion kernel/util/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ func ChatGPT(msg string, contextMsgs []string, c *openai.Client, model string, m
return
}

func NewOpenAIClient(apiKey, apiProxy, apiBaseURL, apiUserAgent string) *openai.Client {
func NewOpenAIClient(apiKey, apiProxy, apiBaseURL, apiUserAgent, apiVersion, apiProvider string) *openai.Client {
config := openai.DefaultConfig(apiKey)
if "Azure" == apiProvider {
config = openai.DefaultAzureConfig(apiKey, apiBaseURL)
config.APIVersion = apiVersion
}

transport := &http.Transport{}
if "" != apiProxy {
proxyUrl, err := url.Parse(apiProxy)
Expand Down

0 comments on commit 77885a8

Please sign in to comment.