-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
50 lines (35 loc) · 1.09 KB
/
utils.py
File metadata and controls
50 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
import os
import subprocess
import sys
import codecs
import io
import re
import json
import pickle
import numpy as np
from scipy import spatial
from config import OPENAI_API_KEY
class myOpenAI():
def __init__(self):
from openai import OpenAI
self.client = OpenAI(api_key=OPENAI_API_KEY)
self.embedding_name = "text-embedding-3-large"
def get_embedding(self, text):
#text = text.replace("\n", " ") #??
return self.client.embeddings.create(input = [text], model=self.embedding_name).data[0].embedding
def file_put_contents(filename, st):
file = codecs.open(filename, "w", "utf-8")
file.write(st)
file.close()
def file_get_contents(name):
f = io.open(name, mode="r", encoding="utf-8") #utf-8 | Windows-1252
return f.read()
def pickle_save(path, obj):
with open(path, "wb") as file: pickle.dump(obj, file)
def pickle_load(path):
with open(path, "rb") as file:
obj = pickle.load(file)
return obj
def cosine_similarity(v1, v2):
return 1 - spatial.distance.cosine(v1, v2)