1. Build a basic chatbot using Gemini 2.0 flash

This commit is contained in:
Julien
2025-09-24 13:45:00 +02:00
parent f15b420006
commit f76aad00ba
13 changed files with 280 additions and 2 deletions

1
.env.template Normal file
View File

@@ -0,0 +1 @@
GOOGLE_API_KEY=your_google_api_key_here

1
.gitignore vendored
View File

@@ -136,6 +136,7 @@ venv/
ENV/
env.bak/
venv.bak/
Scripts/
# Spyder project settings
.spyderproject

View File

@@ -0,0 +1,164 @@
/* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
/* Greenlet object interface */
#ifndef Py_GREENLETOBJECT_H
#define Py_GREENLETOBJECT_H
#include <Python.h>
#ifdef __cplusplus
extern "C" {
#endif
/* This is deprecated and undocumented. It does not change. */
#define GREENLET_VERSION "1.0.0"
#ifndef GREENLET_MODULE
#define implementation_ptr_t void*
#endif
typedef struct _greenlet {
PyObject_HEAD
PyObject* weakreflist;
PyObject* dict;
implementation_ptr_t pimpl;
} PyGreenlet;
#define PyGreenlet_Check(op) (op && PyObject_TypeCheck(op, &PyGreenlet_Type))
/* C API functions */
/* Total number of symbols that are exported */
#define PyGreenlet_API_pointers 12
#define PyGreenlet_Type_NUM 0
#define PyExc_GreenletError_NUM 1
#define PyExc_GreenletExit_NUM 2
#define PyGreenlet_New_NUM 3
#define PyGreenlet_GetCurrent_NUM 4
#define PyGreenlet_Throw_NUM 5
#define PyGreenlet_Switch_NUM 6
#define PyGreenlet_SetParent_NUM 7
#define PyGreenlet_MAIN_NUM 8
#define PyGreenlet_STARTED_NUM 9
#define PyGreenlet_ACTIVE_NUM 10
#define PyGreenlet_GET_PARENT_NUM 11
#ifndef GREENLET_MODULE
/* This section is used by modules that uses the greenlet C API */
static void** _PyGreenlet_API = NULL;
# define PyGreenlet_Type \
(*(PyTypeObject*)_PyGreenlet_API[PyGreenlet_Type_NUM])
# define PyExc_GreenletError \
((PyObject*)_PyGreenlet_API[PyExc_GreenletError_NUM])
# define PyExc_GreenletExit \
((PyObject*)_PyGreenlet_API[PyExc_GreenletExit_NUM])
/*
* PyGreenlet_New(PyObject *args)
*
* greenlet.greenlet(run, parent=None)
*/
# define PyGreenlet_New \
(*(PyGreenlet * (*)(PyObject * run, PyGreenlet * parent)) \
_PyGreenlet_API[PyGreenlet_New_NUM])
/*
* PyGreenlet_GetCurrent(void)
*
* greenlet.getcurrent()
*/
# define PyGreenlet_GetCurrent \
(*(PyGreenlet * (*)(void)) _PyGreenlet_API[PyGreenlet_GetCurrent_NUM])
/*
* PyGreenlet_Throw(
* PyGreenlet *greenlet,
* PyObject *typ,
* PyObject *val,
* PyObject *tb)
*
* g.throw(...)
*/
# define PyGreenlet_Throw \
(*(PyObject * (*)(PyGreenlet * self, \
PyObject * typ, \
PyObject * val, \
PyObject * tb)) \
_PyGreenlet_API[PyGreenlet_Throw_NUM])
/*
* PyGreenlet_Switch(PyGreenlet *greenlet, PyObject *args)
*
* g.switch(*args, **kwargs)
*/
# define PyGreenlet_Switch \
(*(PyObject * \
(*)(PyGreenlet * greenlet, PyObject * args, PyObject * kwargs)) \
_PyGreenlet_API[PyGreenlet_Switch_NUM])
/*
* PyGreenlet_SetParent(PyObject *greenlet, PyObject *new_parent)
*
* g.parent = new_parent
*/
# define PyGreenlet_SetParent \
(*(int (*)(PyGreenlet * greenlet, PyGreenlet * nparent)) \
_PyGreenlet_API[PyGreenlet_SetParent_NUM])
/*
* PyGreenlet_GetParent(PyObject* greenlet)
*
* return greenlet.parent;
*
* This could return NULL even if there is no exception active.
* If it does not return NULL, you are responsible for decrementing the
* reference count.
*/
# define PyGreenlet_GetParent \
(*(PyGreenlet* (*)(PyGreenlet*)) \
_PyGreenlet_API[PyGreenlet_GET_PARENT_NUM])
/*
* deprecated, undocumented alias.
*/
# define PyGreenlet_GET_PARENT PyGreenlet_GetParent
# define PyGreenlet_MAIN \
(*(int (*)(PyGreenlet*)) \
_PyGreenlet_API[PyGreenlet_MAIN_NUM])
# define PyGreenlet_STARTED \
(*(int (*)(PyGreenlet*)) \
_PyGreenlet_API[PyGreenlet_STARTED_NUM])
# define PyGreenlet_ACTIVE \
(*(int (*)(PyGreenlet*)) \
_PyGreenlet_API[PyGreenlet_ACTIVE_NUM])
/* Macro that imports greenlet and initializes C API */
/* NOTE: This has actually moved to ``greenlet._greenlet._C_API``, but we
keep the older definition to be sure older code that might have a copy of
the header still works. */
# define PyGreenlet_Import() \
{ \
_PyGreenlet_API = (void**)PyCapsule_Import("greenlet._C_API", 0); \
}
#endif /* GREENLET_MODULE */
#ifdef __cplusplus
}
#endif
#endif /* !Py_GREENLETOBJECT_H */

View File

@@ -1,3 +1,11 @@
# test_langgraph
# Test of LangGraph
First test of LangGraph
First test of LangGraph following the documentation
Step 1 : follow the LangGraph tutorial "Build a custom workflow"
Step 2 : use the base to create a different custom workflow
To use, install all dependancies : requirements.txt
Add your Google Gemini API key in .env
Run : python -m main
Exit with "quit", "exit", "q"

35
main.py Normal file
View File

@@ -0,0 +1,35 @@
# from src.graph_builder import build_graph
from src.graph_builder import graph_builder
from src.nodes import chatbot
from langgraph.graph import START, END
# The first argument is the unique node name
# The second argument is the function or object that will be called whenever
# the node is used.
if __name__ == "__main__":
graph_builder.add_node("chatbot", chatbot)
graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("chatbot", END)
graph = graph_builder.compile()
def stream_graph_updates(user_input: str):
for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}):
for value in event.values():
print("Assistant:", value["messages"][-1].content)
while True:
try:
user_input = input("User: ")
if user_input.lower() in ["quit", "exit", "q"]:
print("Goodbye!")
break
stream_graph_updates(user_input)
except:
# fallback if input() is not available
user_input = "What do you know about LangGraph?"
print("User: " + user_input)
stream_graph_updates(user_input)
break

5
pyvenv.cfg Normal file
View File

@@ -0,0 +1,5 @@
home = C:\Users\julie\AppData\Local\Programs\Python\Python313
include-system-site-packages = false
version = 3.13.0
executable = C:\Users\julie\AppData\Local\Programs\Python\Python313\python.exe
command = C:\Users\julie\AppData\Local\Programs\Python\Python313\python.exe -m venv C:\Users\julie\projects\test_langgraph

BIN
requirements.txt Normal file

Binary file not shown.

0
src/__init__.py Normal file
View File

32
src/graph_builder.py Normal file
View File

@@ -0,0 +1,32 @@
# Defines and builds the StateGraph
from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
class State(TypedDict):
# Messages have the type "list". The `add_messages` function
# in the annotation defines how this state key should be updated
# (in this case, it appends messages to the list, rather than overwriting them)
messages: Annotated[list, add_messages]
graph_builder = StateGraph(State)
# from langgraph import Graph
# from src.nodes import CustomNode
# def build_graph():
# graph = Graph()
# # Add nodes and edges here
# node = CustomNode()
# graph.add_node(node)
# return graph

21
src/nodes.py Normal file
View File

@@ -0,0 +1,21 @@
import os
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
from langgraph.graph import StateGraph
from src.graph_builder import State
load_dotenv()
#os.environ["GOOGLE_API_KEY"] = "..."
llm = init_chat_model("google_genai:gemini-2.0-flash")
def chatbot(state: State):
return {"messages": [llm.invoke(state["messages"])]}
# class CustomNode:
# def __init__(self):
# pass
# def run(self):
# print("Custom node running")

1
src/utils.py Normal file
View File

@@ -0,0 +1 @@
# Utility functions for LangGraph project

0
tests/__init__.py Normal file
View File

10
tests/test_graph.py Normal file
View File

@@ -0,0 +1,10 @@
import unittest
from src.graph_builder import build_graph
class TestGraph(unittest.TestCase):
def test_graph_runs(self):
graph = build_graph()
self.assertIsNotNone(graph)
if __name__ == "__main__":
unittest.main()