Ewan Claude commited on
Commit
f3f4d58
·
1 Parent(s): 7fb6a87

Add LangChain v1 chatbot with Groq API integration

Browse files

Implemented a public Gradio chat interface using LangChain v1 with Groq's GPT-OSS-20B model. The app includes message history support, public sharing, and requires no HuggingFace login for users.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (4) hide show
  1. .gitignore +50 -0
  2. README.md +44 -12
  3. app.py +85 -0
  4. requirements.txt +5 -0
.gitignore ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Environment variables
2
+ .env
3
+ .env.local
4
+ .env.*.local
5
+
6
+ # Python
7
+ __pycache__/
8
+ *.py[cod]
9
+ *$py.class
10
+ *.so
11
+ .Python
12
+ build/
13
+ develop-eggs/
14
+ dist/
15
+ downloads/
16
+ eggs/
17
+ .eggs/
18
+ lib/
19
+ lib64/
20
+ parts/
21
+ sdist/
22
+ var/
23
+ wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+
28
+ # Virtual environments
29
+ venv/
30
+ ENV/
31
+ env/
32
+
33
+ # IDE
34
+ .vscode/
35
+ .idea/
36
+ *.swp
37
+ *.swo
38
+ *~
39
+
40
+ # OS
41
+ .DS_Store
42
+ Thumbs.db
43
+
44
+ # Gradio
45
+ gradio_cached_examples/
46
+ flagged/
47
+ .gradio/
48
+
49
+ # Claude Code
50
+ .claude/
README.md CHANGED
@@ -1,12 +1,44 @@
1
- ---
2
- title: Test
3
- emoji: 🏃
4
- colorFrom: blue
5
- colorTo: red
6
- sdk: gradio
7
- sdk_version: 5.49.1
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # LangChain Groq Chat Demo
2
+
3
+ A simple chat interface using LangChain v1 with Groq's GPT-OSS-20B model and Gradio.
4
+
5
+ ## Setup
6
+
7
+ 1. Get a Groq API key from [console.groq.com](https://console.groq.com)
8
+ 2. Set your API key as an environment variable or create a `.env` file:
9
+ ```
10
+ GROQ_API_KEY=your_groq_api_key_here
11
+ ```
12
+
13
+ ## Running Locally
14
+
15
+ ```bash
16
+ pip install -r requirements.txt
17
+ python app.py
18
+ ```
19
+
20
+ The app will launch with:
21
+ - A public shareable URL (valid for 72 hours)
22
+ - Local access at `http://localhost:7860`
23
+ - Network access at `http://0.0.0.0:7860`
24
+
25
+ ## Deploying to Hugging Face Spaces
26
+
27
+ 1. Create a new **Public** Space on Hugging Face (select "Public" visibility)
28
+ 2. Select "Gradio" as the SDK
29
+ 3. Upload these files to your Space: `app.py`, `requirements.txt`
30
+ 4. Add your `GROQ_API_KEY` in the Space settings under "Variables and secrets"
31
+ 5. The Space will automatically deploy and be publicly accessible without requiring login
32
+
33
+ ## Files
34
+
35
+ - `app.py` - Main Gradio application with LangChain v1 integration
36
+ - `requirements.txt` - Python dependencies (LangChain v1 compatible)
37
+ - `README.md` - This file
38
+
39
+ ## Features
40
+
41
+ - Uses OpenAI's GPT-OSS-20B model via Groq API (fast inference)
42
+ - LangChain v1 with message history support
43
+ - Public sharing enabled by default
44
+ - No Hugging Face login required to use the app
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from langchain_groq import ChatGroq
4
+ from langchain_core.messages import HumanMessage, AIMessage
5
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
6
+ from langchain_core.runnables.history import RunnableWithMessageHistory
7
+ from langchain_community.chat_message_histories import ChatMessageHistory
8
+ from dotenv import load_dotenv
9
+
10
+ load_dotenv()
11
+
12
+ # Store for chat histories
13
+ chat_histories = {}
14
+
15
+ def get_session_history(session_id: str):
16
+ if session_id not in chat_histories:
17
+ chat_histories[session_id] = ChatMessageHistory()
18
+ return chat_histories[session_id]
19
+
20
+ def create_chain():
21
+ groq_api_key = os.getenv("GROQ_API_KEY")
22
+ if not groq_api_key:
23
+ raise ValueError("GROQ_API_KEY environment variable is not set")
24
+
25
+ llm = ChatGroq(
26
+ model="openai/gpt-oss-20b",
27
+ temperature=0.7,
28
+ max_retries=2,
29
+ )
30
+
31
+ prompt = ChatPromptTemplate.from_messages([
32
+ ("system", "You are a helpful AI assistant. Be concise and helpful."),
33
+ MessagesPlaceholder(variable_name="history"),
34
+ ("human", "{input}"),
35
+ ])
36
+
37
+ chain = prompt | llm
38
+
39
+ return RunnableWithMessageHistory(
40
+ chain,
41
+ get_session_history,
42
+ input_messages_key="input",
43
+ history_messages_key="history",
44
+ )
45
+
46
+ conversation_chain = create_chain()
47
+
48
+ def chat(message, history):
49
+ try:
50
+ response = conversation_chain.invoke(
51
+ {"input": message},
52
+ config={"configurable": {"session_id": "default"}}
53
+ )
54
+ history.append([message, response.content])
55
+ return "", history
56
+ except Exception as e:
57
+ error_msg = f"Error: {str(e)}"
58
+ history.append([message, error_msg])
59
+ return "", history
60
+
61
+ with gr.Blocks(title="LangChain Groq Chat Demo") as demo:
62
+ gr.Markdown("# LangChain + Groq Chat Demo")
63
+ gr.Markdown("Chat with OpenAI's GPT-OSS-20B model using LangChain v1")
64
+
65
+ chatbot = gr.Chatbot(height=400)
66
+ msg = gr.Textbox(
67
+ placeholder="Type your message here...",
68
+ label="Message",
69
+ scale=4
70
+ )
71
+
72
+ with gr.Row():
73
+ submit = gr.Button("Send", variant="primary")
74
+ clear = gr.Button("Clear")
75
+
76
+ msg.submit(chat, [msg, chatbot], [msg, chatbot])
77
+ submit.click(chat, [msg, chatbot], [msg, chatbot])
78
+ clear.click(lambda: ([], ""), outputs=[chatbot, msg])
79
+
80
+ if __name__ == "__main__":
81
+ demo.launch(
82
+ share=True, # Creates a public URL that anyone can access
83
+ server_name="0.0.0.0", # Makes it accessible on network
84
+ server_port=7860 # Standard port for Gradio apps
85
+ )
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ langchain==1.0.8
2
+ langchain-community==0.4.1
3
+ langchain-groq==1.0.1
4
+ gradio>=4.44.1
5
+ python-dotenv==1.0.0