新增了调用llm做语义方向判断

This commit is contained in:
mxr612 2024-05-16 19:17:19 +08:00
parent 2f758ba5db
commit b9c88ca4c0
2 changed files with 19 additions and 10 deletions

View File

@ -9,9 +9,7 @@ from sklearn.metrics.pairwise import cosine_similarity
from openai import OpenAI from openai import OpenAI
os.environ["OPENAI_API_KEY"]= "sk-PRJ811XeKzEy20Ug3dA98a34Af8b40B5816dE15503D33599" client = OpenAI(api_key="sk-PRJ811XeKzEy20Ug3dA98a34Af8b40B5816dE15503D33599",base_url="http://154.9.28.247:3000/v1/")
os.environ["OPENAI_BASE_URL"]= "http://154.9.28.247:3000/v1/"
client = OpenAI()
def batch(): def batch():
scales = os.listdir("Scales") scales = os.listdir("Scales")
@ -44,6 +42,17 @@ def old_type(str):
with open(str,"w") as file: with open(str,"w") as file:
file.write(json.dumps(new)) file.write(json.dumps(new))
def liguistic(a:str,b:str):
completion = client.chat.completions.create(
model="large",
messages=[
{"role": "system", "content": "请严格按照要求回答,不要有多余解释。\n判断这两句话的语意是否相反相反返回True否则返回False。"},
{"role": "user", "content": a+'\n'+b}
]
)
print(completion.choices[0].message.content)
return 'True' in completion.choices[0].message.content
def calc_similarity(scale): def calc_similarity(scale):
item=[] item=[]
vec=[] vec=[]
@ -56,6 +65,9 @@ def calc_similarity(scale):
que=[] que=[]
for i,v in enumerate(simi): for i,v in enumerate(simi):
for j in range(0,i): for j in range(0,i):
if liguistic(scale[item[i]],scale[item[j]]):
simi[i][j]*=-1
print("Opposite:",scale[item[i]],scale[item[j]])
que.append({"from":item[j], "to":item[i], "similarity":simi[i][j]}) que.append({"from":item[j], "to":item[i], "similarity":simi[i][j]})
return que return que
@ -96,14 +108,13 @@ def correlation():
if i!=j: if i!=j:
try: try:
que[i,j]=data[i].corr(data[j]) que[i,j]=data[i].corr(data[j])
if i in rev: # if i in rev:
que[i,j]*=-1 # que[i,j]*=-1
if j in rev: # if j in rev:
que[i,j]*=-1 # que[i,j]*=-1
que[j,i]=que[i,j] que[j,i]=que[i,j]
except: except:
pass pass
else: else:
break break
return que return que

View File

@ -20,8 +20,6 @@ for i in similarity:
s+=str(i["similarity"])+','+str(correlation[i["from"],i["to"]])+'\n' s+=str(i["similarity"])+','+str(correlation[i["from"],i["to"]])+'\n'
x.append(i["similarity"]) x.append(i["similarity"])
y.append(correlation[i["from"],i["to"]]) y.append(correlation[i["from"],i["to"]])
if(i['similarity']>0.5 and correlation[i['from'],i['to']]<-0.5):
print(i,correlation[i['from'],i['to']])
except: except:
pass pass