- 转用json对象方式存储

- 新增文件存储计算结果
This commit is contained in:
mxr612 2024-05-14 12:08:37 +08:00
parent b69975d124
commit 8717b81bbc
2 changed files with 14 additions and 18 deletions

View File

@ -14,18 +14,9 @@ def embedding(s:str):
return
else:
return client.embeddings.create(
input=s, model="text-embedding-3-small" # nomic-embed-text text-embedding-3-small
input=s, model="nomic-embed-text" # nomic-embed-text text-embedding-3-small
).data[0].embedding
class edge:
a:str
b:str
sim:float
def __init__(self, a:str, b:str, sim:float):
self.a=a
self.b=b
self.sim=sim
def main(scale:json):
item=[]
@ -42,9 +33,9 @@ def main(scale:json):
for i,v in enumerate(simi):
for j in range(0,i):
# print(simi[i][j],',',item[i],',',item[j])
que.append(edge(item[i], item[j], simi[i][j]))
que.append({"from":item[j], "i":item[i], "similarity":simi[i][j]})
# print("\n")
sorted(que,key= lambda t : t.sim)
sorted(que,key= lambda t : t["similarity"])
return que

15
main.py
View File

@ -1,10 +1,15 @@
from ItemRelate import main as relate
import json
import os
with open("TestFile/adhd.json") as json_file:
if os.path.getsize("TestFile/items.json") == 0:
with open("TestFile/adhd.json","r") as json_file:
scale = json.load(json_file)
que=relate(scale)
with open("TestFile/items.json","w") as items:
items.write(json.dumps(que))
else:
with open("TestFile/items.json","r") as items:
que = json.load(items)
que=relate(scale)
for i in que:
print(i.a, i.b, i.sim)
print(que)