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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# This script demonstrates using LangChain, OpenRouter, and weather/news tools in Python.
|
| 3 |
+
# Purpose: Fetch news, search weather, and interact with LLM agents using LangChain.
|
| 4 |
+
|
| 5 |
+
# Import necessary libraries for LangChain, OpenAI, RSS parsing, HTML cleaning, embeddings, and HTTP requests
|
| 6 |
+
from langchain.agents import initialize_agent
|
| 7 |
+
from langchain.tools import Tool
|
| 8 |
+
from langchain_openai import ChatOpenAI # Import ChatOpenAI from langchain_openai
|
| 9 |
+
import feedparser # For parsing RSS feeds
|
| 10 |
+
from bs4 import BeautifulSoup # For cleaning HTML
|
| 11 |
+
from langchain.schema import Document # For document schema
|
| 12 |
+
from langchain_community.vectorstores import FAISS # For vector storage
|
| 13 |
+
from requests import get # For HTTP requests
|
| 14 |
+
from langchain.agents import AgentType # For agent type
|
| 15 |
+
from langchain.memory import ConversationBufferWindowMemory
|
| 16 |
+
from langchain_community.embeddings import CohereEmbeddings
|
| 17 |
+
from dotenv import load_dotenv
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
load_dotenv("./settings.env")
|
| 21 |
+
|
| 22 |
+
os.environ['LANGSMITH_TRACING_V2'] = os.getenv('LANGSMITH_TRACING_V2')
|
| 23 |
+
os.environ['LANGSMITH_API_KEY'] = os.getenv('LANGSMITH_API_KEY')
|
| 24 |
+
os.environ['LANGSMITH_ENDPOINT'] = os.getenv('LANGSMITH_ENDPOINT')
|
| 25 |
+
os.environ['LANGSMITH_PROJECT'] = os.getenv('LANGSMITH_PROJECT')
|
| 26 |
+
API_KEY_OPENAI =os.getenv('OPENROUTER_API')
|
| 27 |
+
API_KEY_COHERE = os.getenv('COHERE_API')
|
| 28 |
+
API_KEY_WEATHER = os.getenv('WEATHER_API')
|
| 29 |
+
|
| 30 |
+
# Initialize the ChatOpenAI model with OpenRouter API key and endpoint
|
| 31 |
+
llm = ChatOpenAI(model_name='gpt-4o-mini',api_key=API_KEY_OPENAI,base_url="https://openrouter.ai/api/v1")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Function to fetch news from an RSS feed URL
|
| 36 |
+
def rss_getNews(feed_url):
|
| 37 |
+
feed = feedparser.parse(feed_url)
|
| 38 |
+
return [entry['title'] + "\n" + entry['summary'] for entry in feed.entries]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Function to clean HTML content and extract text
|
| 42 |
+
def clean_html(raw_html):
|
| 43 |
+
soup = BeautifulSoup(raw_html, "html.parser")
|
| 44 |
+
return soup.get_text(separator="\n")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# List of RSS feed URLs to fetch news from
|
| 48 |
+
urls = [
|
| 49 |
+
'https://www.hespress.com/feed',
|
| 50 |
+
'https://alyaoum24.com/feed',
|
| 51 |
+
'https://al3omk.com/feed'
|
| 52 |
+
]
|
| 53 |
+
# Parse feeds and create Document objects for each news entry
|
| 54 |
+
documents = []
|
| 55 |
+
for url in urls:
|
| 56 |
+
feed = feedparser.parse(url)
|
| 57 |
+
for entry in feed.entries[:30]: # Limit to 10 entries per feed
|
| 58 |
+
tags = [tag.term for tag in entry.tags] if hasattr(entry, 'tags') else []
|
| 59 |
+
content = f"{entry.title}\n{entry.published}\n{entry.summary}"
|
| 60 |
+
metadata = {"tags": tags}
|
| 61 |
+
documents.append(Document(page_content=clean_html(content), metadata=metadata))
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Initialize Cohere embeddings
|
| 66 |
+
embedding = CohereEmbeddings(cohere_api_key=API_KEY_COHERE,model="embed-multilingual-v3.0",user_agent="langchain")
|
| 67 |
+
# Create FAISS vector store from documents and save locally
|
| 68 |
+
db = FAISS.from_documents(documents, embedding)
|
| 69 |
+
db.save_local('data_news')
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# Create a retriever from the FAISS vector store
|
| 74 |
+
retriever = db.as_retriever()
|
| 75 |
+
# Define a tool for filtered news search using the retriever
|
| 76 |
+
def filtered_retriever_tool():
|
| 77 |
+
def search_func(query):
|
| 78 |
+
results = retriever.get_relevant_documents(query)
|
| 79 |
+
return results
|
| 80 |
+
return Tool(
|
| 81 |
+
name='News Search',
|
| 82 |
+
func=search_func,
|
| 83 |
+
description="Search news articles by topic and tag"
|
| 84 |
+
)
|
| 85 |
+
# Initialize the filtered news search tool
|
| 86 |
+
filtered_tool = filtered_retriever_tool()
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# Format weather data with emojis and print nicely
|
| 93 |
+
def format_weather_with_emoji(weather_data, city_name="Casablanca"):
|
| 94 |
+
weather_desc = weather_data["weather"][0]["description"]
|
| 95 |
+
temp = weather_data["main"]["temp"]
|
| 96 |
+
feels_like = weather_data["main"]["feels_like"]
|
| 97 |
+
humidity = weather_data["main"]["humidity"]
|
| 98 |
+
wind_speed = weather_data["wind"]["speed"]
|
| 99 |
+
|
| 100 |
+
# Map weather descriptions to emojis
|
| 101 |
+
emoji_map = {
|
| 102 |
+
"clear sky": "☀️",
|
| 103 |
+
"few clouds": "🌤️",
|
| 104 |
+
"scattered clouds": "⛅",
|
| 105 |
+
"broken clouds": "☁️",
|
| 106 |
+
"shower rain": "🌧️",
|
| 107 |
+
"rain": "🌦️",
|
| 108 |
+
"thunderstorm": "⛈️",
|
| 109 |
+
"snow": "❄️",
|
| 110 |
+
"mist": "🌫️"
|
| 111 |
+
}
|
| 112 |
+
emoji = emoji_map.get(weather_desc.lower(), "🌈") # Default emoji
|
| 113 |
+
result = (
|
| 114 |
+
f"🌆 Weather in {city_name}:\n"
|
| 115 |
+
f" - Condition: {weather_desc} {emoji}\n"
|
| 116 |
+
f" - Temperature: {temp}°C (feels like {feels_like}°C)\n"
|
| 117 |
+
f" - Humidity: {humidity}%\n"
|
| 118 |
+
f" - Wind speed: {wind_speed} m/s\n"
|
| 119 |
+
f"Have a great day! 😊"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
return result
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
# Function to get coordinates (latitude, longitude) for a city using OpenWeatherMap
|
| 128 |
+
def get_coordinates(city: str):
|
| 129 |
+
"""Automatically fetch coordinates from city name"""
|
| 130 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&APPID={API_KEY_WEATHER}"
|
| 131 |
+
response = get(url)
|
| 132 |
+
if response.status_code == 200 and response.json():
|
| 133 |
+
data = response.json()['coord']
|
| 134 |
+
return data['lat'],data['lon']
|
| 135 |
+
else:
|
| 136 |
+
return response.json()
|
| 137 |
+
# Function to search weather for a city and format the result
|
| 138 |
+
def weather_search_tool(city, part: str = ""):
|
| 139 |
+
lat,lon =get_coordinates(city)
|
| 140 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&APPID={API_KEY_WEATHER}"
|
| 141 |
+
response = get(url)
|
| 142 |
+
if response.status_code == 200:
|
| 143 |
+
return format_weather_with_emoji(response.json(),city)
|
| 144 |
+
else:
|
| 145 |
+
return {"error": response.status_code, "message": response.text}
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
# Create a Tool for weather search with emoji formatting
|
| 149 |
+
weather_search = Tool(
|
| 150 |
+
name='weather search',
|
| 151 |
+
func=weather_search_tool,
|
| 152 |
+
description="🔍 Search weather conditions by location 🌍. The tool fetches current weather data using latitude and longitude coordinates 📍 and explains results with fun emojis ☀️🌧️🌬️. The output is formatted neatly in multiple lines for better readability."
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
# Initialize conversation memory for the agent
|
| 157 |
+
memory = ConversationBufferWindowMemory(memory_key='chat_history',return_messages=True,k=8)
|
| 158 |
+
# Initialize the conversational agent with tools and memory
|
| 159 |
+
agent = initialize_agent(tools=[filtered_tool,weather_search],llm=llm,memory=memory,agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION)
|
| 160 |
+
|
| 161 |
+
def sendPromptToAgent(prompt:str):
|
| 162 |
+
# Invoke the agent with a sample query and print the output
|
| 163 |
+
out= agent.invoke(prompt)
|
| 164 |
+
return str(out)
|
api.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from agent_api import sendPromptToAgent
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# تحميل المتغيرات من .env
|
| 8 |
+
load_dotenv("./settings.env")
|
| 9 |
+
|
| 10 |
+
# دالة لمعالجة الإدخال من Gradio
|
| 11 |
+
def handle_prompt(prompt):
|
| 12 |
+
if not prompt.strip():
|
| 13 |
+
return "❌ الرجاء إدخال نص."
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
result = sendPromptToAgent(prompt)
|
| 17 |
+
return result
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"❌ حدث خطأ: {e}"
|
| 20 |
+
|
| 21 |
+
# تصميم واجهة Gradio
|
| 22 |
+
iface = gr.Interface(
|
| 23 |
+
fn=handle_prompt,
|
| 24 |
+
inputs=gr.Textbox(lines=3, placeholder="اكتب سؤالك هنا...", label="📨 إدخال النص"),
|
| 25 |
+
outputs=gr.Textbox(label="💡 الرد من الوكيل"),
|
| 26 |
+
title="📰 وكيل الأخبار",
|
| 27 |
+
description="أدخل سؤالك ليقوم الوكيل بالإجابة عليه باستخدام LangChain.",
|
| 28 |
+
theme="soft"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# تشغيل التطبيق
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|