feat: allow to set LocalRAG API URL ad key (#61)
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
committed by
GitHub
parent
08785e2908
commit
a83f4512b6
@@ -37,6 +37,11 @@ type AgentConfig struct {
|
||||
// This is what needs to be part of ActionsConfig
|
||||
Model string `json:"model" form:"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"`
|
||||
HUD bool `json:"hud" form:"hud"`
|
||||
StandaloneJob bool `json:"standalone_job" form:"standalone_job"`
|
||||
|
||||
@@ -27,7 +27,7 @@ type AgentPool struct {
|
||||
agents map[string]*Agent
|
||||
managers map[string]sse.Manager
|
||||
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
|
||||
connectors func(*AgentConfig) []Connector
|
||||
promptBlocks func(*AgentConfig) []PromptBlock
|
||||
@@ -182,6 +182,22 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
||||
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)
|
||||
promptBlocks := a.promptBlocks(config)
|
||||
|
||||
@@ -231,7 +247,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
||||
WithCharacterFile(characterFile),
|
||||
WithLLMAPIKey(a.apiKey),
|
||||
WithTimeout(a.timeout),
|
||||
WithRAGDB(localrag.NewWrappedClient(a.localRAGAPI, name)),
|
||||
WithRAGDB(localrag.NewWrappedClient(a.localRAGAPI, a.localRAGKey, name)),
|
||||
WithAgentReasoningCallback(func(state ActionCurrentState) bool {
|
||||
xlog.Info(
|
||||
"Agent is thinking",
|
||||
|
||||
@@ -26,9 +26,9 @@ type WrappedClient struct {
|
||||
collection string
|
||||
}
|
||||
|
||||
func NewWrappedClient(baseURL, collection string) *WrappedClient {
|
||||
func NewWrappedClient(baseURL, apiKey, collection string) *WrappedClient {
|
||||
wc := &WrappedClient{
|
||||
Client: NewClient(baseURL),
|
||||
Client: NewClient(baseURL, apiKey),
|
||||
collection: collection,
|
||||
}
|
||||
|
||||
@@ -104,15 +104,25 @@ type Result struct {
|
||||
// Client is a client for the RAG API
|
||||
type Client struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
}
|
||||
|
||||
// NewClient creates a new RAG API client
|
||||
func NewClient(baseURL string) *Client {
|
||||
func NewClient(baseURL, apiKey string) *Client {
|
||||
return &Client{
|
||||
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
|
||||
func (c *Client) CreateCollection(name string) error {
|
||||
url := fmt.Sprintf("%s/api/collections", c.BaseURL)
|
||||
@@ -126,7 +136,15 @@ func (c *Client) CreateCollection(name string) error {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
@@ -143,7 +161,14 @@ func (c *Client) CreateCollection(name string) error {
|
||||
func (c *Client) ListCollections() ([]string, error) {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -162,18 +187,25 @@ func (c *Client) ListCollections() ([]string, error) {
|
||||
return collections, nil
|
||||
}
|
||||
|
||||
// ListCollections lists all collections
|
||||
// ListEntries lists all entries in a collection
|
||||
func (c *Client) ListEntries(collection string) ([]string, error) {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("failed to list collections")
|
||||
return nil, errors.New("failed to list entries")
|
||||
}
|
||||
|
||||
var entries []string
|
||||
@@ -185,39 +217,37 @@ func (c *Client) ListEntries(collection string) ([]string, error) {
|
||||
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) {
|
||||
url := fmt.Sprintf("%s/api/collections/%s/entry/delete", c.BaseURL, collection)
|
||||
|
||||
type request struct {
|
||||
Entry string `json:"entry"`
|
||||
}
|
||||
client := &http.Client{}
|
||||
|
||||
payload, err := json.Marshal(request{Entry: entry})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest("DELETE", url, bytes.NewBuffer(payload))
|
||||
req, err := http.NewRequest(http.MethodDelete, url, bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
c.addAuthHeader(req)
|
||||
|
||||
// Fetch Request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
bodyResult := new(bytes.Buffer)
|
||||
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
|
||||
@@ -243,7 +273,15 @@ func (c *Client) Search(collection, query string, maxResults int) ([]Result, 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 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -262,12 +300,15 @@ func (c *Client) Search(collection, query string, maxResults int) ([]Result, err
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// Reset resets a collection
|
||||
func (c *Client) Reset(collection string) error {
|
||||
url := fmt.Sprintf("%s/api/collections/%s/reset", c.BaseURL, collection)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.addAuthHeader(req)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
@@ -279,7 +320,6 @@ func (c *Client) Reset(collection string) error {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b := new(bytes.Buffer)
|
||||
b.ReadFrom(resp.Body)
|
||||
|
||||
return errors.New("failed to reset collection: " + b.String())
|
||||
}
|
||||
|
||||
@@ -319,6 +359,7 @@ func (c *Client) Store(collection, filePath string) error {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
c.addAuthHeader(req)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
|
||||
@@ -174,6 +174,26 @@
|
||||
<input type="text" name="multimodal_model" id="multimodal_model" placeholder="Model name">
|
||||
</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">
|
||||
<label for="enable_reasoning" class="checkbox-label">
|
||||
<span class="checkbox-custom">
|
||||
|
||||
@@ -345,6 +345,10 @@
|
||||
document.getElementById('periodic_runs').value = config.periodic_runs || '';
|
||||
document.getElementById('model').value = config.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('system_prompt').value = config.system_prompt || '';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user