-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
610a0b8
commit d9d9170
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package jina | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type Config struct { | ||
url string | ||
|
||
token string | ||
model string | ||
|
||
client *http.Client | ||
} | ||
|
||
type Option func(*Config) | ||
|
||
func WithClient(client *http.Client) Option { | ||
return func(c *Config) { | ||
c.client = client | ||
} | ||
} | ||
|
||
func WithToken(token string) Option { | ||
return func(c *Config) { | ||
c.token = token | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package jina | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/adrianliechti/llama/pkg/provider" | ||
) | ||
|
||
var _ provider.Embedder = (*Embedder)(nil) | ||
|
||
type Embedder struct { | ||
*Config | ||
} | ||
|
||
func NewEmbedder(url string, options ...Option) (*Embedder, error) { | ||
if url == "" { | ||
url = "https://api.jina.ai" | ||
} | ||
|
||
url = strings.TrimRight(url, "/") | ||
url = strings.TrimSuffix(url, "/v1") | ||
|
||
cfg := &Config{ | ||
client: http.DefaultClient, | ||
|
||
url: url, | ||
|
||
model: "jina-clip-v1", | ||
} | ||
|
||
for _, option := range options { | ||
option(cfg) | ||
} | ||
|
||
return &Embedder{ | ||
Config: cfg, | ||
}, nil | ||
} | ||
|
||
func (e *Embedder) Embed(ctx context.Context, content string) (*provider.Embedding, error) { | ||
body := map[string]any{ | ||
"input": []string{ | ||
strings.TrimSpace(content), | ||
}, | ||
} | ||
|
||
u, _ := url.JoinPath(e.url, "/v1/embeddings") | ||
|
||
req, _ := http.NewRequestWithContext(ctx, "POST", u, jsonReader(body)) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
if e.token != "" { | ||
req.Header.Set("Authorization", "Bearer "+e.token) | ||
} | ||
|
||
resp, err := e.client.Do(req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, convertError(resp) | ||
} | ||
|
||
var result EmbeddingList | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(result.Data) == 0 { | ||
return nil, errors.New("no embeddings found") | ||
} | ||
|
||
return &provider.Embedding{ | ||
Data: result.Data[0].Embedding, | ||
}, nil | ||
} | ||
|
||
type EmbeddingList struct { | ||
Object string `json:"object"` | ||
|
||
Model string `json:"model"` | ||
Data []Embedding `json:"data"` | ||
} | ||
|
||
type Embedding struct { | ||
Object string `json:"object"` | ||
|
||
Index int `json:"index"` | ||
Embedding []float32 `json:"embedding"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package jina | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func convertError(resp *http.Response) error { | ||
data, _ := io.ReadAll(resp.Body) | ||
|
||
if len(data) == 0 { | ||
return errors.New(http.StatusText(resp.StatusCode)) | ||
} | ||
|
||
return errors.New(string(data)) | ||
} | ||
|
||
func jsonReader(v any) io.Reader { | ||
b := new(bytes.Buffer) | ||
|
||
enc := json.NewEncoder(b) | ||
enc.SetEscapeHTML(false) | ||
|
||
enc.Encode(v) | ||
return b | ||
} |