feat: allow to set LocalRAG API URL ad key (#61)

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2025-03-19 23:10:14 +01:00
committed by GitHub
parent 08785e2908
commit a83f4512b6
5 changed files with 119 additions and 33 deletions

View File

@@ -37,6 +37,11 @@ type AgentConfig struct {
// This is what needs to be part of ActionsConfig // This is what needs to be part of ActionsConfig
Model string `json:"model" form:"model"` Model string `json:"model" form:"model"`
MultimodalModel string `json:"multimodal_model" form:"multimodal_model"` MultimodalModel string `json:"multimodal_model" form:"multimodal_model"`
APIURL string `json:"api_url" form:"api_url"`
APIKey string `json:"api_key" form:"api_key"`
LocalRAGURL string `json:"local_rag_url" form:"local_rag_url"`
LocalRAGAPIKey string `json:"local_rag_api_key" form:"local_rag_api_key"`
Name string `json:"name" form:"name"` Name string `json:"name" form:"name"`
HUD bool `json:"hud" form:"hud"` HUD bool `json:"hud" form:"hud"`
StandaloneJob bool `json:"standalone_job" form:"standalone_job"` StandaloneJob bool `json:"standalone_job" form:"standalone_job"`

View File

@@ -27,7 +27,7 @@ type AgentPool struct {
agents map[string]*Agent agents map[string]*Agent
managers map[string]sse.Manager managers map[string]sse.Manager
agentStatus map[string]*Status agentStatus map[string]*Status
apiURL, defaultModel, defaultMultimodalModel, localRAGAPI, apiKey string apiURL, defaultModel, defaultMultimodalModel, localRAGAPI, localRAGKey, apiKey string
availableActions func(*AgentConfig) func(ctx context.Context, pool *AgentPool) []Action availableActions func(*AgentConfig) func(ctx context.Context, pool *AgentPool) []Action
connectors func(*AgentConfig) []Connector connectors func(*AgentConfig) []Connector
promptBlocks func(*AgentConfig) []PromptBlock promptBlocks func(*AgentConfig) []PromptBlock
@@ -182,6 +182,22 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
config.PeriodicRuns = "10m" config.PeriodicRuns = "10m"
} }
if config.APIURL != "" {
a.apiURL = config.APIURL
}
if config.APIKey != "" {
a.apiKey = config.APIKey
}
if config.LocalRAGURL != "" {
a.localRAGAPI = config.LocalRAGURL
}
if config.LocalRAGAPIKey != "" {
a.localRAGKey = config.LocalRAGAPIKey
}
connectors := a.connectors(config) connectors := a.connectors(config)
promptBlocks := a.promptBlocks(config) promptBlocks := a.promptBlocks(config)
@@ -231,7 +247,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
WithCharacterFile(characterFile), WithCharacterFile(characterFile),
WithLLMAPIKey(a.apiKey), WithLLMAPIKey(a.apiKey),
WithTimeout(a.timeout), WithTimeout(a.timeout),
WithRAGDB(localrag.NewWrappedClient(a.localRAGAPI, name)), WithRAGDB(localrag.NewWrappedClient(a.localRAGAPI, a.localRAGKey, name)),
WithAgentReasoningCallback(func(state ActionCurrentState) bool { WithAgentReasoningCallback(func(state ActionCurrentState) bool {
xlog.Info( xlog.Info(
"Agent is thinking", "Agent is thinking",

View File

@@ -26,9 +26,9 @@ type WrappedClient struct {
collection string collection string
} }
func NewWrappedClient(baseURL, collection string) *WrappedClient { func NewWrappedClient(baseURL, apiKey, collection string) *WrappedClient {
wc := &WrappedClient{ wc := &WrappedClient{
Client: NewClient(baseURL), Client: NewClient(baseURL, apiKey),
collection: collection, collection: collection,
} }
@@ -104,15 +104,25 @@ type Result struct {
// Client is a client for the RAG API // Client is a client for the RAG API
type Client struct { type Client struct {
BaseURL string BaseURL string
APIKey string
} }
// NewClient creates a new RAG API client // NewClient creates a new RAG API client
func NewClient(baseURL string) *Client { func NewClient(baseURL, apiKey string) *Client {
return &Client{ return &Client{
BaseURL: baseURL, BaseURL: baseURL,
APIKey: apiKey,
} }
} }
// Add a helper method to set the Authorization header
func (c *Client) addAuthHeader(req *http.Request) {
if c.APIKey == "" {
return
}
req.Header.Set("Authorization", "Bearer "+c.APIKey)
}
// CreateCollection creates a new collection // CreateCollection creates a new collection
func (c *Client) CreateCollection(name string) error { func (c *Client) CreateCollection(name string) error {
url := fmt.Sprintf("%s/api/collections", c.BaseURL) url := fmt.Sprintf("%s/api/collections", c.BaseURL)
@@ -126,7 +136,15 @@ func (c *Client) CreateCollection(name string) error {
return err return err
} }
resp, err := http.Post(url, "application/json", bytes.NewBuffer(payload)) req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payload))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
c.addAuthHeader(req)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { if err != nil {
return err return err
} }
@@ -143,7 +161,14 @@ func (c *Client) CreateCollection(name string) error {
func (c *Client) ListCollections() ([]string, error) { func (c *Client) ListCollections() ([]string, error) {
url := fmt.Sprintf("%s/api/collections", c.BaseURL) url := fmt.Sprintf("%s/api/collections", c.BaseURL)
resp, err := http.Get(url) req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
c.addAuthHeader(req)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -162,18 +187,25 @@ func (c *Client) ListCollections() ([]string, error) {
return collections, nil return collections, nil
} }
// ListCollections lists all collections // ListEntries lists all entries in a collection
func (c *Client) ListEntries(collection string) ([]string, error) { func (c *Client) ListEntries(collection string) ([]string, error) {
url := fmt.Sprintf("%s/api/collections/%s/entries", c.BaseURL, collection) url := fmt.Sprintf("%s/api/collections/%s/entries", c.BaseURL, collection)
resp, err := http.Get(url) req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
c.addAuthHeader(req)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return nil, errors.New("failed to list collections") return nil, errors.New("failed to list entries")
} }
var entries []string var entries []string
@@ -185,39 +217,37 @@ func (c *Client) ListEntries(collection string) ([]string, error) {
return entries, nil return entries, nil
} }
// DeleteEntry deletes an Entry in a collection and return the entries left // DeleteEntry deletes an entry in a collection
func (c *Client) DeleteEntry(collection, entry string) ([]string, error) { func (c *Client) DeleteEntry(collection, entry string) ([]string, error) {
url := fmt.Sprintf("%s/api/collections/%s/entry/delete", c.BaseURL, collection) url := fmt.Sprintf("%s/api/collections/%s/entry/delete", c.BaseURL, collection)
type request struct { type request struct {
Entry string `json:"entry"` Entry string `json:"entry"`
} }
client := &http.Client{}
payload, err := json.Marshal(request{Entry: entry}) payload, err := json.Marshal(request{Entry: entry})
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Create request req, err := http.NewRequest(http.MethodDelete, url, bytes.NewBuffer(payload))
req, err := http.NewRequest("DELETE", url, bytes.NewBuffer(payload))
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
c.addAuthHeader(req)
// Fetch Request client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
bodyResult := new(bytes.Buffer) bodyResult := new(bytes.Buffer)
bodyResult.ReadFrom(resp.Body) bodyResult.ReadFrom(resp.Body)
return nil, errors.New("failed to delete collection: " + bodyResult.String()) return nil, errors.New("failed to delete entry: " + bodyResult.String())
} }
var results []string var results []string
@@ -243,7 +273,15 @@ func (c *Client) Search(collection, query string, maxResults int) ([]Result, err
return nil, err return nil, err
} }
resp, err := http.Post(url, "application/json", bytes.NewBuffer(payload)) req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
c.addAuthHeader(req)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -262,12 +300,15 @@ func (c *Client) Search(collection, query string, maxResults int) ([]Result, err
return results, nil return results, nil
} }
// Reset resets a collection
func (c *Client) Reset(collection string) error { func (c *Client) Reset(collection string) error {
url := fmt.Sprintf("%s/api/collections/%s/reset", c.BaseURL, collection) url := fmt.Sprintf("%s/api/collections/%s/reset", c.BaseURL, collection)
req, err := http.NewRequest(http.MethodPost, url, nil) req, err := http.NewRequest(http.MethodPost, url, nil)
if err != nil { if err != nil {
return err return err
} }
c.addAuthHeader(req)
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
@@ -279,7 +320,6 @@ func (c *Client) Reset(collection string) error {
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
b := new(bytes.Buffer) b := new(bytes.Buffer)
b.ReadFrom(resp.Body) b.ReadFrom(resp.Body)
return errors.New("failed to reset collection: " + b.String()) return errors.New("failed to reset collection: " + b.String())
} }
@@ -319,6 +359,7 @@ func (c *Client) Store(collection, filePath string) error {
return err return err
} }
req.Header.Set("Content-Type", writer.FormDataContentType()) req.Header.Set("Content-Type", writer.FormDataContentType())
c.addAuthHeader(req)
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)

View File

@@ -174,6 +174,26 @@
<input type="text" name="multimodal_model" id="multimodal_model" placeholder="Model name"> <input type="text" name="multimodal_model" id="multimodal_model" placeholder="Model name">
</div> </div>
<div class="mb-4">
<label for="api_url">API URL </label>
<input type="text" name="api_url" id="api_url" placeholder="API URL">
</div>
<div class="mb-4">
<label for="api_key">API Key </label>
<input type="text" name="api_key" id="api_key" placeholder="API Key">
</div>
<div class="mb-4">
<label for="local_rag_url">LocalRAG API URL </label>
<input type="text" name="local_rag_url" id="local_rag_url" placeholder="LocalRAG API URL">
</div>
<div class="mb-4">
<label for="local_rag_api_key">LocalRAG API Key </label>
<input type="text" name="local_rag_api_key" id="local_rag_api_key" placeholder="LocalRAG API Key">
</div>
<div class="mb-4"> <div class="mb-4">
<label for="enable_reasoning" class="checkbox-label"> <label for="enable_reasoning" class="checkbox-label">
<span class="checkbox-custom"> <span class="checkbox-custom">

View File

@@ -345,6 +345,10 @@
document.getElementById('periodic_runs').value = config.periodic_runs || ''; document.getElementById('periodic_runs').value = config.periodic_runs || '';
document.getElementById('model').value = config.model || ''; document.getElementById('model').value = config.model || '';
document.getElementById('multimodal_model').value = config.multimodal_model || ''; document.getElementById('multimodal_model').value = config.multimodal_model || '';
document.getElementById('api_url').value = config.api_url || '';
document.getElementById('api_key').value = config.api_key || '';
document.getElementById('local_rag_url').value = config.local_rag_url || '';
document.getElementById('local_rag_api_key').value = config.local_rag_token || '';
document.getElementById('permanent_goal').value = config.permanent_goal || ''; document.getElementById('permanent_goal').value = config.permanent_goal || '';
document.getElementById('system_prompt').value = config.system_prompt || ''; document.getElementById('system_prompt').value = config.system_prompt || '';