Skip to content

Commit 6eda003

Browse files
committed
feat: use identifier for the feed, change llm, update swagger
1 parent d2f1fb0 commit 6eda003

File tree

6 files changed

+230
-62
lines changed

6 files changed

+230
-62
lines changed

api/api.gen.go

Lines changed: 49 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger.yaml

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ paths:
2929
$ref: '#/components/schemas/Error'
3030
x-stoplight:
3131
id: e6uenhk2m21k5
32-
parameters:
33-
- schema:
34-
type: boolean
35-
in: query
36-
name: bySimilarity
32+
parameters: []
3733
parameters:
3834
- schema:
3935
type: string
@@ -86,11 +82,50 @@ paths:
8682
schema:
8783
type: object
8884
properties: {}
85+
'/v1/search/feed/similarities/{feedID}':
86+
get:
87+
summary: Your GET endpoint
88+
tags: []
89+
responses:
90+
'200':
91+
description: OK
92+
content:
93+
application/json:
94+
schema:
95+
$ref: '#/components/schemas/FeedDetails'
96+
'400':
97+
description: Bad Request
98+
'500':
99+
description: Internal Server Error
100+
operationId: get-v1-search-feed-similarities
101+
x-stoplight:
102+
id: cu8bexmv3qwv6
103+
parameters: []
104+
parameters:
105+
- schema:
106+
type: string
107+
name: feedID
108+
in: path
109+
required: true
89110
components:
90111
schemas:
91112
FeedItem:
92113
type: object
114+
x-stoplight:
115+
id: kwewfmf4xgq3e
116+
required:
117+
- id
118+
- title
119+
- summary
120+
- link
121+
- source
122+
- language
123+
- date
93124
properties:
125+
id:
126+
type: string
127+
x-stoplight:
128+
id: i41a7r351wuiy
94129
title:
95130
type: string
96131
summary:
@@ -107,15 +142,10 @@ components:
107142
date:
108143
type: string
109144
format: date-time
110-
required:
111-
- title
112-
- summary
113-
- link
114-
- source
115-
- language
116-
- date
117-
x-stoplight:
118-
id: kwewfmf4xgq3e
145+
'':
146+
type: string
147+
x-stoplight:
148+
id: 6uzhltl5f1ayg
119149
FeedImage:
120150
type: object
121151
properties:
@@ -209,3 +239,17 @@ components:
209239
required:
210240
- commit
211241
- version
242+
FeedDetails:
243+
title: FeedDetails
244+
x-stoplight:
245+
id: jdfavk4q6qd6o
246+
type: object
247+
properties:
248+
source:
249+
$ref: '#/components/schemas/FeedItem'
250+
similarities:
251+
type: array
252+
x-stoplight:
253+
id: 0s2id9mhqzyv9
254+
items:
255+
$ref: '#/components/schemas/FeedItem'

internal/app/feed.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66

77
type FeedRepository interface {
88
// Search returns the results of a search query.
9-
Find(query string) ([]Feed, error)
9+
FindByKeyword(query string) ([]Feed, error)
1010
// Search returns the results of a search query by similarity.
11-
FindBySimilarity(query string) ([]Feed, error)
11+
FindBySimilarity(doc Feed) ([]Feed, error)
12+
FindByID(id string) (Feed, error)
1213
// Index indexes a document.
1314
Save(doc Feed) error
1415
// Update a document in index.
@@ -20,6 +21,7 @@ type FeedRepository interface {
2021
}
2122

2223
type Feed struct {
24+
ID string `json:"id"`
2325
Title string `json:"title"`
2426
Link string `json:"link"`
2527
Language string `json:"language"`

internal/repository/typesense/feed.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package typesense
22

33
import (
44
"context"
5+
"crypto/sha256"
6+
"encoding/hex"
7+
"fmt"
58
"time"
69

710
"github.com/typesense/typesense-go/typesense"
@@ -21,7 +24,7 @@ func NewFeedRepository(client *typesense.Client) *FeedRepository {
2124
}
2225
}
2326

24-
func (f *FeedRepository) Find(query string) ([]app.Feed, error) {
27+
func (f *FeedRepository) FindByKeyword(query string) ([]app.Feed, error) {
2528
searchParameters := &api.SearchCollectionParams{
2629
Q: query,
2730
QueryBy: "title, summary",
@@ -41,6 +44,7 @@ func (f *FeedRepository) Find(query string) ([]app.Feed, error) {
4144
}
4245

4346
f := app.Feed{
47+
ID: doc["id"].(string),
4448
Title: doc["title"].(string),
4549
Link: doc["link"].(string),
4650
Source: doc["source"].(string),
@@ -55,11 +59,38 @@ func (f *FeedRepository) Find(query string) ([]app.Feed, error) {
5559
return feeds, nil
5660
}
5761

62+
func (f *FeedRepository) FindByID(id string) (app.Feed, error) {
63+
d, err := f.client.Collection("feeds").Document(id).Retrieve(f.ctx)
64+
if err != nil {
65+
return app.Feed{}, err
66+
}
67+
68+
date, err := time.Parse(time.RFC3339, d["date"].(string))
69+
if err != nil {
70+
return app.Feed{}, err
71+
}
72+
73+
fStruct := app.Feed{
74+
ID: d["id"].(string),
75+
Title: d["title"].(string),
76+
Link: d["link"].(string),
77+
Source: d["source"].(string),
78+
Language: d["language"].(string),
79+
Summary: d["summary"].(string),
80+
Date: date,
81+
}
5882

59-
func (f *FeedRepository) FindBySimilarity(query string) ([]app.Feed, error){
83+
return fStruct, nil
84+
}
85+
86+
func (f *FeedRepository) FindBySimilarity(feed app.Feed) ([]app.Feed, error) {
87+
vQ := fmt.Sprintf("title_summary_embedding:([], id: %s, distance_threshold:0.183)", feed.ID)
88+
eF := "title_summary_embedding"
6089
searchParameters := &api.SearchCollectionParams{
61-
Q: query,
62-
QueryBy: "title_summary_embedding",
90+
Q: feed.Title,
91+
QueryBy: "title,title_summary_embedding",
92+
VectorQuery: &vQ,
93+
ExcludeFields: &eF,
6394
}
6495
searchResult, err := f.client.Collection("feeds").Documents().Search(f.ctx, searchParameters)
6596
if err != nil {
@@ -90,10 +121,9 @@ func (f *FeedRepository) FindBySimilarity(query string) ([]app.Feed, error){
90121
return feeds, nil
91122
}
92123

93-
94124
func (f *FeedRepository) Save(doc app.Feed) error {
95125
docMap := map[string]interface{}{
96-
"id": doc.Link,
126+
"id": generateUniqueID(doc.Link),
97127
"title": doc.Title,
98128
"link": doc.Link,
99129
"source": doc.Source,
@@ -117,7 +147,7 @@ func (f *FeedRepository) Update(docs ...app.Feed) error {
117147
for i, doc := range docs {
118148
// Convert app.Feed to map[string]interface{} for updating
119149
docMap := map[string]interface{}{
120-
"id": doc.Link,
150+
"id": generateUniqueID(doc.Link),
121151
"title": doc.Title,
122152
"link": doc.Link,
123153
"source": doc.Source,
@@ -160,3 +190,13 @@ func (f *FeedRepository) Delete(doc app.Feed) error {
160190

161191
return nil
162192
}
193+
194+
func generateUniqueID(link string) string {
195+
hash := sha256.New()
196+
hash.Write([]byte(link))
197+
hashBytes := hash.Sum(nil)
198+
199+
uniqueID := hex.EncodeToString(hashBytes)
200+
201+
return uniqueID
202+
}

internal/repository/typesense/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func GetFeedSchema(client *typesense.Client) *api.CollectionSchema {
5151
ModelName string "json:\"model_name\""
5252
ProjectId *string "json:\"project_id,omitempty\""
5353
}{
54-
ModelName: "ts/all-MiniLM-L12-v2",
54+
ModelName: "ts/multilingual-e5-large",
5555
},
5656
},
5757
},

0 commit comments

Comments
 (0)