aboutsummaryrefslogtreecommitdiff
o

yf@sdZddlZddlZddlZddlmZddlmZddlZddlZddl	m
Z
ddlmZdZ
dd	Zd
dZdd
ZddZddZdddZdddZddZddZddZdS)zGthis module contains code for processing response from fahamu client.pyN)urljoin)quote)GeneNetworkQAClient)DocIDsz*https://genenetwork.fahamuai.com/api/taskscCsNt|tr
|dSt|tr%|dd|dd|dd|ddS|S)z)Function for formatting bibliography infoz.txtauthor.titleyeardoi )
isinstancestrremovesuffixdict)bib_infor>/home/shebes/code/GN-LLM-Hacking/gnqa_eval/src/apis/process.pyformat_bibliography_infos



,rcCstddd|DS)z1helper function for filtering non-printable charscSsg|]}|tjvrt|qSr)string	printabler
).0charrrr
<listcomp> sz(filter_response_text.<locals>.<listcomp>)jsonloadsjoin)valrrrfilter_response_texts
rc
Csfg}|D]*\}}d}|D]
}|d|d7}q||}||kr#|n||}	|||	|dq|S)z!function to parse doc_ids contentr	text)doc_idbibInfocomboTxt)itemsappend)
context
get_info_funcformat_bib_funcresultsdoc_idssummary	combo_txtentrydoc_inforrrr
parse_context$s
r/c	
Csz)ttd|d|d|}dd|i}tj||d}|ddi|WStjjyF}ztd	|j	j
d
|j	j|d}~wty[}z
tdt
||d}~ww)
zJThis method is used to provide feedback for a document by making a rating.z/feedback?task_id=z
&document_id=z
&feedback=
AuthorizationzBearer )headersstatussuccessz HTTP Error Occurred:            z -with status code- NzAn error occurred: )rBASE_URLrequestspostraise_for_statusr
exceptions	HTTPErrorRuntimeErrorresponser status_code	Exceptionr
)	task_idr!rating
auth_tokenurlr1resp
http_errorerrorrrr
rate_document3s*rEcCsbtj||}tj|st|dt|d
}t|WdS1s*wYdS)z#function to open and load json filez  was not found or is a directoryrbN)ospathrisfileFileNotFoundErroropenrload)filenamedir_path	file_pathfile_handlerrrr	load_fileFs$rQrcCsjz"t|tj|d}|D]}||dr||d|d<q
|WSty4td|||YSw)z3method to fetch and populate references with pubmedzgn-meta/litr!pubmedz$failed to find pubmed_path for %s/%s)rQrGrHrgetrJloggingrD)
references	file_namedata_dirrR	referencerrrfetch_pubmedOsrYcCstt|d}|dt||\}}|dkr!tdt|||\}}|dkrVt|j	}|
ddur=|dgfS|dd	}|dd
}	t|	tj
t}
|||
fS|dgfS)z)entry function for the gn3 api endpoint()api_keyz?ask=rz Error connecting to Fahamu Api: dataN8Please try to rephrase your question to receive feedbackanswerr&)rr5Sessionaskrr:r

get_answerrr rSr/rgetInfor)queryr@tmp_dir
api_clientresr>r3	resp_textr_r&rUrrrget_gnqa_s 




ric	Cstt|d}||\}}|dkr=t|j}|ddur$|dgfS|dd}|dd}t|tj	t
}|||fS|dgfS)NrZr\r]r^r_r&)rr5r`r_rr rSr/rrcr)	r@r>rfrgr3rhr_r&rUrrrget_response_from_taskidus




rjcCs2|d|d|}|rt|S|dgddS)z,this method fetches prev user query searchesLLM:-zSorry No answer for youN)rdr_rUr>)rSrr)rduser_id
redis_connresultrrrfetch_query_resultss
rpcCs*|d|d}dddd|DDS)z0methods to fetch all queries for a specific userrk*cSsg|]}|dkr|qSrr)rrdrrrrsz$get_user_queries.<locals>.<listcomp>cSsg|]	}|ddqS)rl)	partition)rrorrrrs)keys)rmrnr)rrrget_user_queriessrvrr)__doc__rGrrurllib.parserrrTr5apis.gnqaclientr	apis.resprr4rrr/rErQrYrirjrprvrrrr<module>s*