mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-23 00:34:27 +01:00
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/ddworken/hishtory/client/data"
|
|
"github.com/ddworken/hishtory/client/hctx"
|
|
"github.com/ddworken/hishtory/client/lib"
|
|
"github.com/ddworken/hishtory/shared/ai"
|
|
)
|
|
|
|
var mostRecentQuery string
|
|
|
|
func DebouncedGetAiSuggestions(ctx context.Context, query string, numberCompletions int) ([]string, error) {
|
|
mostRecentQuery = query
|
|
time.Sleep(time.Millisecond * 300)
|
|
if mostRecentQuery == query {
|
|
return GetAiSuggestions(ctx, query, numberCompletions)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func GetAiSuggestions(ctx context.Context, query string, numberCompletions int) ([]string, error) {
|
|
if os.Getenv("OPENAI_API_KEY") == "" {
|
|
return GetAiSuggestionsViaHishtoryApi(ctx, query, numberCompletions)
|
|
} else {
|
|
suggestions, _, err := ai.GetAiSuggestionsViaOpenAiApi(query, numberCompletions)
|
|
return suggestions, err
|
|
}
|
|
}
|
|
|
|
func GetAiSuggestionsViaHishtoryApi(ctx context.Context, query string, numberCompletions int) ([]string, error) {
|
|
req := ai.AiSuggestionRequest{
|
|
DeviceId: hctx.GetConf(ctx).DeviceId,
|
|
UserId: data.UserId(hctx.GetConf(ctx).UserSecret),
|
|
Query: query,
|
|
NumberCompletions: numberCompletions,
|
|
}
|
|
reqData, err := json.Marshal(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to marshal AiSuggestionRequest: %w", err)
|
|
}
|
|
respData, err := lib.ApiPost(ctx, "/api/v1/ai-suggest", "application/json", reqData)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to query /api/v1/ai-suggest: %w", err)
|
|
}
|
|
var resp ai.AiSuggestionResponse
|
|
err = json.Unmarshal(respData, &resp)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse /api/v1/ai-suggest response: %w", err)
|
|
}
|
|
return resp.Suggestions, nil
|
|
}
|