Skip to content

Commit

Permalink
feat: add by similarity
Browse files Browse the repository at this point in the history
  • Loading branch information
luigibarbato committed Mar 12, 2024
1 parent d386774 commit 6db5f94
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 75 deletions.
17 changes: 4 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,15 @@ WORKDIR /data
COPY --from=builder /app/main /app/main

ARG UNCONDITIONAL_API_SOURCE_REPO
ARG UNCONDITIONAL_API_SOURCE_CLIENT_KEY
ARG UNCONDITIONAL_API_FEED_REPO_HOST
ARG UNCONDITIONAL_API_FEED_REPO_KEY
ARG UNCONDITIONAL_API_FEED_REPO_INDEX
ARG UNCONDITIONAL_API_LOG_ENV

ENV UNCONDITIONAL_API_SOURCE_REPO=${UNCONDITIONAL_API_SOURCE_REPO}
ENV UNCONDITIONAL_API_SOURCE_CLIENT_KEY=${UNCONDITIONAL_API_SOURCE_CLIENT_KEY}
ENV UNCONDITIONAL_API_FEED_REPO_HOST=${UNCONDITIONAL_API_FEED_REPO_HOST}
ENV UNCONDITIONAL_API_FEED_REPO_KEY=${UNCONDITIONAL_API_FEED_REPO_KEY}
ENV UNCONDITIONAL_API_FEED_REPO_INDEX=${UNCONDITIONAL_API_FEED_REPO_INDEX}
ENV UNCONDITIONAL_API_LOG_ENV=${UNCONDITIONAL_API_LOG_ENV}

RUN --mount=type=secret,id=UNCONDITIONAL_API_SOURCE_CLIENT_KEY \
--mount=type=secret,id=UNCONDITIONAL_API_FEED_REPO_HOST \
--mount=type=secret,id=UNCONDITIONAL_API_FEED_REPO_INDEX \
--mount=type=secret,id=UNCONDITIONAL_API_FEED_REPO_KEY \
UNCONDITIONAL_API_SOURCE_CLIENT_KEY="$(cat /run/secrets/UNCONDITIONAL_API_SOURCE_CLIENT_KEY)" \
UNCONDITIONAL_API_FEED_REPO_HOST="$(cat /run/secrets/UNCONDITIONAL_API_FEED_REPO_HOST)" \
Expand All @@ -55,18 +50,14 @@ ARG UNCONDITIONAL_API_ADDRESS
ARG UNCONDITIONAL_API_ALLOWED_ORIGINS
ARG UNCONDITIONAL_API_PORT
ARG UNCONDITIONAL_API_SOURCE_REPO
ARG UNCONDITIONAL_API_SOURCE_CLIENT_KEY
ARG UNCONDITIONAL_API_LOG_ENV
ARG UNCONDITIONAL_API_FEED_REPO_HOST
ARG UNCONDITIONAL_API_FEED_REPO_KEY
ARG UNCONDITIONAL_API_FEED_REPO_INDEX

ENV UNCONDITIONAL_API_ADDRESS=${UNCONDITIONAL_API_ADDRESS}
ENV UNCONDITIONAL_API_ALLOWED_ORIGINS=${UNCONDITIONAL_API_ALLOWED_ORIGINS}
ENV UNCONDITIONAL_API_PORT=${UNCONDITIONAL_API_PORT}
ENV UNCONDITIONAL_API_SOURCE_REPO=${UNCONDITIONAL_API_SOURCE_REPO}
ENV UNCONDITIONAL_API_SOURCE_CLIENT_KEY=${UNCONDITIONAL_API_SOURCE_CLIENT_KEY}
ENV UNCONDITIONAL_API_LOG_ENV=${UNCONDITIONAL_API_LOG_ENV}
ENV UNCONDITIONAL_API_FEED_REPO_HOST=${UNCONDITIONAL_API_FEED_REPO_HOST}
ENV UNCONDITIONAL_API_FEED_REPO_KEY=${UNCONDITIONAL_API_FEED_REPO_KEY}
ENV UNCONDITIONAL_API_FEED_REPO_INDEX=${UNCONDITIONAL_API_FEED_REPO_INDEX}

ENTRYPOINT ["./app/main","serve", "--address", "0.0.0.0", "--port","8080","--feed-repo-index","feeds"]
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ test-integration:
build:
@go build --tags=release -o ${_PROJECT_DIRECTORY}/bin/unconditional-server

.PHONY: deploy

deploy:
@sh ./scripts/deploy.sh

# Helpers
check-variable-%: # detection of undefined variables.
@[[ "${${*}}" ]] || (echo '*** Please define variable `${*}` ***' && exit 1)
50 changes: 32 additions & 18 deletions api/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 46 additions & 41 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,80 +1,85 @@
openapi: 3.0.2
x-stoplight:
id: 44w9u0405b53e
id: g0km90jnbo4i0
info:
title: Unconditional
version: "1.0"
version: '1.0'
servers:
- url: "https://api.unconditional.day/v1"
- url: "http://localhost:8080"
- url: "127.0.0.1:8080"
- url: 'https://api.unconditional.day/v1'
- url: 'http://localhost:8080'
- url: '127.0.0.1:8080'
paths:
"/v1/search/feed/{query}":
'/v1/search/feed/{query}':
get:
responses:
"200":
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/FeedItem"
"500":
$ref: '#/components/schemas/FeedItem'
'500':
description: Internal Server Error
content:
application/json:
schema:
type: object
$ref: "#/components/schemas/Error"
parameters:
- name: query
in: path
required: true
schema:
type: string
$ref: '#/components/schemas/Error'
x-stoplight:
id: wosl2fd4xhhrd
"/v1/search/context/{query}":
id: e6uenhk2m21k5
parameters:
- schema:
type: boolean
in: query
name: bySimilarity
parameters:
- schema:
type: string
name: query
in: path
required: true
'/v1/search/context/{query}':
get:
responses:
"200":
'200':
description: OK
content:
application/json:
schema:
type: object
$ref: "#/components/schemas/SearchContextDetails"
"500":
$ref: '#/components/schemas/SearchContextDetails'
'500':
description: Internal Server Error
content:
application/json:
schema:
type: object
$ref: "#/components/schemas/Error"
$ref: '#/components/schemas/Error'
parameters:
- name: query
in: path
required: true
schema:
type: string
x-stoplight:
id: idoe8qr80ebxd
"/v1/version":
id: 7c394v6kr5tff
/v1/version:
get:
summary: Your GET endpoint
tags: []
responses:
"200":
'200':
description: OK
content:
application/json:
schema:
type: object
$ref: "#/components/schemas/ServerVersion"
$ref: '#/components/schemas/ServerVersion'
operationId: get-v1-version
x-stoplight:
id: q4lvzczpgn9wh
id: oucep4v9ubq2z
requestBody:
content:
application/json:
Expand All @@ -98,7 +103,7 @@ components:
type: string
image:
type: object
$ref: "#/components/schemas/FeedImage"
$ref: '#/components/schemas/FeedImage'
date:
type: string
format: date-time
Expand All @@ -110,7 +115,7 @@ components:
- language
- date
x-stoplight:
id: xa4xmb6bpguaf
id: kwewfmf4xgq3e
FeedImage:
type: object
properties:
Expand All @@ -122,11 +127,11 @@ components:
- url
- title
x-stoplight:
id: cmmybtqamyqiy
id: albynchitcqzu
SearchContextDetails:
type: object
x-stoplight:
id: 2bea7d1686f73
id: fhfrlaebk9ask
properties:
title:
type: string
Expand Down Expand Up @@ -156,51 +161,51 @@ components:
- message
- code
x-stoplight:
id: ugjrjhvkdmen2
id: mffsjrkb81ot5
ServerVersion:
title: ServerVersion
x-stoplight:
id: 24qo1fm939bg5
id: 4m4ndcimoercb
type: object
properties:
source:
$ref: "#/components/schemas/SourceReleaseVersion"
$ref: '#/components/schemas/SourceReleaseVersion'
build:
$ref: "#/components/schemas/ServerBuildVersion"
$ref: '#/components/schemas/ServerBuildVersion'
required:
- source
- build
SourceReleaseVersion:
title: SourceReleaseVersion
x-stoplight:
id: ufsclw4vu3qp5
id: qrni9cae7qsr6
type: object
properties:
version:
type: string
x-stoplight:
id: t6xp7ev3nprdj
id: g42kpmf59e9sb
lastUpdatedAt:
type: string
x-stoplight:
id: 26hfi8gtj9bdi
id: ozgly07pllzwb
required:
- version
- lastUpdatedAt
ServerBuildVersion:
title: ServerBuildVersion
x-stoplight:
id: p7byzats6s3zt
id: a6g4vsjso6ozw
type: object
properties:
commit:
type: string
x-stoplight:
id: eoik296b0ddt3
id: 48tl37vuwu47p
version:
type: string
x-stoplight:
id: jup15zjavkg3a
id: mwqxrg5t0vvst
required:
- commit
- version
2 changes: 2 additions & 0 deletions internal/app/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
type FeedRepository interface {
// Search returns the results of a search query.
Find(query string) ([]Feed, error)
// Search returns the results of a search query by similarity.
FindBySimilarity(query string) ([]Feed, error)
// Index indexes a document.
Save(doc Feed) error
// Update a document in index.
Expand Down
36 changes: 36 additions & 0 deletions internal/repository/typesense/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,42 @@ func (f *FeedRepository) Find(query string) ([]app.Feed, error) {
return feeds, nil
}


func (f *FeedRepository) FindBySimilarity(query string) ([]app.Feed, error){
searchParameters := &api.SearchCollectionParams{
Q: query,
QueryBy: "title_summary_embedding",
}
searchResult, err := f.client.Collection("feeds").Documents().Search(f.ctx, searchParameters)
if err != nil {
return nil, err
}

feeds := make([]app.Feed, len(*searchResult.Hits))
for i, x := range *searchResult.Hits {
doc := *x.Document

date, err := time.Parse(time.RFC3339, doc["date"].(string))
if err != nil {
return nil, err
}

f := app.Feed{
Title: doc["title"].(string),
Link: doc["link"].(string),
Source: doc["source"].(string),
Language: doc["language"].(string),
Summary: doc["summary"].(string),
Date: date,
}

feeds[i] = f
}

return feeds, nil
}


func (f *FeedRepository) Save(doc app.Feed) error {
docMap := map[string]interface{}{
"id": doc.Link,
Expand Down
Loading

0 comments on commit 6db5f94

Please sign in to comment.