GithubHelp home page GithubHelp logo

pbnewron / langchain_forecast Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pvl/langchain_forecast

0.0 0.0 0.0 9 KB

Langchain tool for time series forecasting

License: MIT License

Python 100.00%

langchain_forecast's Introduction

Forecasting Tool for LangChain AI

This tool adds a simple exponential moving average forecast to the langchain AI. When used with an SQL tool it allows the agent to fetch history data from a database, forecast a future value and then reason with that information.

Installation

Clone this repository then run:

$ python -m pip install .

Usage

The following code will create an agent that has the forecast tool. First export to the shell the OPENAI_API_KEY and your database connection string as DB_CONN.

import os
from langchain.llms.openai import OpenAI
from langchain.sql_database import SQLDatabase
from lcforecast.agentkit import ForecastToolkit
from lcforecast.agentkit.base import create_forecast_agent

llm = OpenAI(temperature=0)
db = SQLDatabase.from_uri(os.environ["DB_CONN"])

toolkit = ForecastToolkit(db=db, llm=llm)

agent_executor = create_forecast_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=True
)

agent_executor.run("Forecast the invoice total for next month based on the last 12 months of history")

Example output for this agent run.

> Entering new AgentExecutor chain...
Action: list_tables_sql_db
Action Input: ""
Observation: <list of db tables>

Thought: I should check the schema of the Invoice table to see what columns I can query
Action: schema_sql_db
Action Input: "Invoice"

Thought: I should query the Invoice table for the total invoice amount for the last 12 months
Action: query_sql_db
Action Input: SELECT DATE_FORMAT(createdAt, '%Y-%m') AS month, SUM(total) AS total FROM Invoice WHERE createdAt > DATE_SUB(NOW(), INTERVAL 12 MONTH) GROUP BY month ORDER BY month DESC LIMIT 10

Thought: I should use the EMA forecasting tool to predict the total invoice amount for June 2023
Action: ema_forecast

Observation: {'forecast_date': '2023-05-31', 'value': 3219.1}
Thought: I now know the final answer
Final Answer: The forecasted invoice total is 3219.1

langchain_forecast's People

Contributors

pvl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.