Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Curio
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jim Wallace
Curio
Merge requests
!5
Allminilm
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Allminilm
AllMiniLM
into
main
Overview
0
Commits
7
Pipelines
1
Changes
1
Merged
Henry Tian
requested to merge
AllMiniLM
into
main
1 year ago
Overview
0
Commits
7
Pipelines
1
Changes
1
Expand
0
0
Merge request reports
Viewing commit
801d9355
Prev
Next
Show latest version
1 file
+
66
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
801d9355
Upload New File
· 801d9355
Henry Tian
authored
1 year ago
Tests/SwiftNLPTests/AllMiniLM_sampleTest.swift
0 → 100644
+
66
−
0
Options
import
XCTest
@testable
import
SwiftNLP
@testable
import
SwiftAnnoy
final
class
BERT_test
:
XCTestCase
{
func
testBasicExample
()
async
throws
{
let
docs
=
[
"The quick brown fox jumps over the lazy dog"
,
"I enjoy taking long walks along the beach at sunset"
,
"Advances in neural networks have enabled new AI capabilities"
,
"The stock market experienced a significant downturn last week"
,
"Cooking a good meal can be both an art and a science"
,
"The exploration of space is both challenging and rewarding"
,
"Machine learning models are becoming increasingly sophisticated"
,
"I love reading about history and ancient civilizations"
]
let
query
=
[
"I like to read about new technology and artificial intelligence"
]
// let docs = ["cat dog", "bee fly"]
var
database_embedding
:
[[
Float
]]
=
[]
var
query_embedding
:
[
Float
]
=
[]
var
embedding_dim
:
Int
=
384
var
model
=
MiniLMEmbeddings
()
query_embedding
=
await
model
.
encode
(
sentence
:
query
[
0
])
!
var
i
=
1
//append sentence embedding to database_embedding
for
string
in
docs
{
if
let
vector
=
await
model
.
encode
(
sentence
:
string
)
{
database_embedding
.
append
(
vector
)
print
(
i
)
i
+=
1
}
else
{
fatalError
(
"Error occurred1"
)
}
}
let
index
=
AnnoyIndex
<
Float
>
(
itemLength
:
embedding_dim
,
metric
:
.
euclidean
)
try
?
index
.
addItems
(
items
:
&
database_embedding
)
try
?
index
.
build
(
numTrees
:
50
)
let
results
=
index
.
getNNsForVector
(
vector
:
&
query_embedding
,
neighbors
:
8
)
if
let
finalresult
=
results
{
let
extractedIndeices
=
finalresult
.
indices
for
index
in
extractedIndeices
{
if
index
<
docs
.
count
{
print
(
docs
[
index
])
}
else
{
print
(
"Index
\(
index
)
out of range."
)
}
}
}
print
(
results
)
}
}
Loading