import dspy import os from dspy.predict.react import Tool from tavily import TavilyClient lm = dspy.LM('ollama_chat/llama3.2', api_base='http://localhost:11434', api_key='') #lm = dspy.LM('ollama_chat/deepseek-r1', api_base='http://localhost:11434', api_key='') #lm = dspy.LM('ollama_chat/qwq', api_base='http://localhost:11434', api_key='') #lm = dspy.LM('ollama_chat/deepscaler', api_base='http://localhost:11434', api_key='') #lm = dspy.LM('huggingface/Qwen/Qwen2.5-Coder-32B-Instruct') #lm = dspy.LM('huggingface/meta-llama/Llama-3.2-1B') #lm = dspy.LM('groq/qwen-qwq-32b') dspy.configure(lm=lm) search_client = TavilyClient(api_key=os.environ["T_TOKEN"]) class RedFlagsGenerator(dspy.Signature): """Generate red flags based on story in fraud topologies""" story: str = dspy.InputField() red_flag: str = dspy.OutputField() class ControlsGenerator(dspy.Signature): """Generate preventive, detective, corrective controls based on red flag""" red_flag: str = dspy.InputField() control: str = dspy.OutputField() def search_wikipedia(query: str) -> list[str]: """Query ColBERT endpoint, which is a knowledge source based on wikipedia data""" results = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')(query, k=1) return [x["text"] for x in results] def control_search(query: str) -> list[str]: """Run a web search to return the top 3 control to verify red flags is a hoax or false positive""" response = search_client.search(query) return [r["content"] for r in response["results"]] class Detective(dspy.Module): """ As a fraud investigator, analyze the following case story to assess the likelihood of fraud. Use a structured framework that includes: Fraudster Profile & Actions Identify the behavioral profile of the fraudster (e.g., boaster, manipulator, deceiver). Map their actions to common fraud typologies (social engineering, impersonation, identity theft, financial asset targeting). Highlight red flags (fake phone numbers, urgent secrecy, impersonation of authorities, threats of jail). Use control to validate whether each red flag is hoax or a false positive. Examples: cross-checking official contact numbers on verified government websites, confirming with independent third parties, requesting written documentation, checking with financial institutions’ fraud units, or using telecom records (Rogers, etc.) for number spoofing. Risk Assessment - Provide a reasoned judgment on whether the case represents fraud, hoax, or a legitimate investigation. - Explain potential impact (identity theft, wealth destruction, reputational damage). """ def __init__(self): self.redFlags_generator = dspy.ChainOfThought(RedFlagsGenerator) self.controls_generator = dspy.ReAct(ControlsGenerator, tools=[Tool(control_search), Tool(search_wikipedia)]) def forward(self, story, **kwargs): fraud = self.redFlags_generator(story=story).red_flag return self.controls_generator(red_flag=fraud).control story = """ Dear Sir, I am DR Lucas Chiebo, the personal Assistance to the late Nigerian Minister of Justice and the Attorney General of the Federal ChiefBola Ige, who was murdered on 23rd of December 2001 by unknown persons. Before he became the minister of Justice and the Attorney General of the Federation, he was once the Minister of Mines and Power. During his time as a Minister of Mines and Power, the Federal Government of Nigeria gave to his ministry the sum of US$200 Million, which is to be used for the completion of the Ajaokuta Steel Industry and the purchasing of electrical transformers and able for the Nigerian Electric Power Authority (NEPA). Then this jobs and the supply of the transformers and cables were done, but of low Quality standard and the transformers he imported were of low quality standard and of low power capacity. Because of the low quality standard of this jobs, the sum of US$22 Million was realized of which he deposited US$15 Million to a security company abroad and was looking for a reliable person or company whom he will transact business with before he meet his un-timely death on the 23rd of December, 2001. He informed the security company about his foreign partner who will be coming to pick the money although no company’s name was given to the security company before he died it was only I and the late Chief Bola Ige knew about the money. Therefore, if you can be trusted in fairness to your honesty to safe keep that money pending when I will be coming to meet you in your country. Please, if you are not interested with this business, kindly inform me As soon as you received this proposals. At the same time, if you are interested, kindly send me as a matter of urgency: your company’s name and address, your private phone and fax number. This will be used to send to you the fund deposit certificate which you will use to claim the money from the Security company. I am awaiting to hear from you. Best regard. DR Lucas Chiebo """ conan = Detective() #dspy.inspect_history() if __name__=='__main__': #recommend(story) print(conan(story=story))