|
|
from huggingface_hub import login |
|
|
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool, Tool, load_tool, InferenceClientModel |
|
|
from smolagents.models import ChatMessage |
|
|
from transformers import pipeline |
|
|
import cohere |
|
|
from gradio_client import Client |
|
|
from newsapi import NewsApiClient |
|
|
import requests |
|
|
import gradio as gr |
|
|
import os |
|
|
from dotenv import load_dotenv |
|
|
import json |
|
|
from transformers import pipeline |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
newsApiKey = os.getenv('NEWSAPI_KEY') |
|
|
|
|
|
|
|
|
|
|
|
COHERE_API_KEY = os.getenv('COHERE_API_KEY') |
|
|
|
|
|
from groq import Groq |
|
|
|
|
|
client = Groq( |
|
|
api_key=os.environ.get("GROQ_API_KEY"), |
|
|
) |
|
|
|
|
|
|
|
|
@tool |
|
|
def get_company_news_articles(company_name: str) -> dict: |
|
|
""" |
|
|
fetch news articles about a company. |
|
|
|
|
|
Args: |
|
|
company_name (str): the company to get news articles about |
|
|
|
|
|
Returns: |
|
|
list: a list of articles about the company |
|
|
""" |
|
|
|
|
|
response = requests.get(f'https://newsapi.org/v2/everything?q={company_name}&from=2025-06-01&sortBy=popularity&apiKey={newsApiKey}') |
|
|
try: |
|
|
data = response.json() |
|
|
except Exception as e: |
|
|
print("β Failed to parse JSON:", str(e)) |
|
|
return [] |
|
|
|
|
|
print("π Full NewsAPI response:", data) |
|
|
|
|
|
if data.get("status") != "ok": |
|
|
print("β NewsAPI error:", data.get("message")) |
|
|
return [] |
|
|
|
|
|
|
|
|
print(f"results from news api get request are: {data.get('articles')}") |
|
|
|
|
|
articles = data.get('articles', []) |
|
|
filtered_articles = [article for article in articles] |
|
|
return filtered_articles |
|
|
|
|
|
|
|
|
|
|
|
class OllamaLLM: |
|
|
pass |
|
|
|
|
|
|
|
|
class grokLLM: |
|
|
pass |
|
|
|
|
|
class mistralLLM: |
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
class CohereLLM: |
|
|
def __init__(self, api_key: str = COHERE_API_KEY, model_name: str = "command-r-plus"): |
|
|
self.client = cohere.Client(api_key) |
|
|
self.model_name = model_name |
|
|
|
|
|
def __call__(self, prompt: str, **kwargs) -> str: |
|
|
|
|
|
kwargs.pop('stop_sequences', None) |
|
|
|
|
|
if isinstance(prompt, list): |
|
|
prompt = self._convert_chat_to_prompt(prompt) |
|
|
|
|
|
|
|
|
temperature = kwargs.get("temperature", 0.7) |
|
|
max_tokens = kwargs.get("max_tokens", 300) |
|
|
|
|
|
response = self.client.generate( |
|
|
model=self.model_name, |
|
|
prompt=prompt, |
|
|
max_tokens=max_tokens, |
|
|
temperature=temperature, |
|
|
) |
|
|
|
|
|
|
|
|
output_text = response.generations[0].text.strip() |
|
|
|
|
|
|
|
|
return ChatMessage(role="assistant", content=output_text) |
|
|
|
|
|
def _convert_chat_to_prompt(self, messages): |
|
|
""" |
|
|
Convert chat-style message history to a single string prompt. |
|
|
Example input: [{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi!"}] |
|
|
""" |
|
|
prompt = "" |
|
|
for message in messages: |
|
|
role = message.get("role", "user") |
|
|
content = message.get("content", "") |
|
|
if role == "user": |
|
|
prompt += f"User: {content}\n" |
|
|
elif role == "assistant": |
|
|
prompt += f"Assistant: {content}\n" |
|
|
else: |
|
|
prompt += f"{role.title()}: {content}\n" |
|
|
prompt += "Assistant:" |
|
|
return prompt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cohere_model = CohereLLM() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mainFunc(articles): |
|
|
if isinstance(articles, str): |
|
|
articles = json.loads(articles) |
|
|
newsApiKey = os.getenv('NEWSAPI_KEY') |
|
|
if not newsApiKey: |
|
|
raise ValueError("Missing NEWS_API_KEY in environment variables.") |
|
|
|
|
|
print(f"data structure of articles is: {articles}") |
|
|
|
|
|
prompt = f""" |
|
|
You are an agent that analyzes the risk factors for a company, by using the data from: |
|
|
- Documents: {articles['company_info']['documents']} |
|
|
- News summaries: {articles['news_data']['articles_summary']} |
|
|
|
|
|
Please respond using **only** the following JSON format: |
|
|
|
|
|
{{ |
|
|
"risk_factors": [ |
|
|
{{ |
|
|
"severity": "Low | Medium | High", |
|
|
"specific_event": "the specific type of event, i. e. nuclear war" |
|
|
"risk_type": "e.g., Operational Risk, Financial Risk, Reputational Risk, Legal Risk", |
|
|
"affected_contracts": "Name or path of the affected document", |
|
|
"affected_clauses": "Clause number(s) or section(s)", |
|
|
"narrative": {{ |
|
|
"solutions_in_contract": "Summarize contractual protections or remedies", |
|
|
"alternative_mitigation_strategies": "Suggest other ways to reduce the risk", |
|
|
"monitoring_tasks": "Define ongoing monitoring or reporting actions" |
|
|
}} |
|
|
}} |
|
|
], |
|
|
"available_data": {{ |
|
|
"context_items": ["Summary of the company info and risk-related context"], |
|
|
"document_count": {len(articles['company_info']['documents'])}, |
|
|
"content_available": true, |
|
|
"news_available": true |
|
|
}} |
|
|
}} |
|
|
""" |
|
|
|
|
|
|
|
|
chat_completion = client.chat.completions.create( |
|
|
messages=[ |
|
|
{ |
|
|
"role": "system", |
|
|
"content": prompt |
|
|
}, |
|
|
{ |
|
|
"role": "user", |
|
|
"content": f"What are the {articles['analysis_request']['analysis_scope']} risk factors for {articles['company_info']['name']} ?", |
|
|
} |
|
|
], |
|
|
model="llama-3.3-70b-versatile", |
|
|
response_format={"type": "json_object"}, |
|
|
) |
|
|
print(chat_completion.choices[0].message.content) |
|
|
return chat_completion.choices[0].message.content |
|
|
|
|
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=mainFunc, |
|
|
inputs="text", |
|
|
outputs="text", |
|
|
title="hackathon agent", |
|
|
description="finds info about a company" |
|
|
) |
|
|
|
|
|
demo.launch(share=True) |