Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
36 changes: 36 additions & 0 deletions 05_src/assignment_chat/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import gradio as gr
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage


load_dotenv(r'C:\Users\pca_9\Desktop\deploying-ai\05_src\.secrets')


gateway_key = os.getenv('API_GATEWAY_KEY')

llm = init_chat_model(
"gpt-4o-mini",
model_provider="openai",
base_url='https://k7uffyg03f.execute-api.us-east-1.amazonaws.com/prod/openai/v1',
default_headers={"x-api-key": gateway_key},
api_key="any_value"
)

def simple_chat(message, history):
# Guardrails: No Cats, Dogs, Horoscopes, or Taylor Swift
system_prompt = SystemMessage(content="You are a witty assistant. You refuse to talk about Taylor Swift, pets, or zodiac signs.")
langchain_messages = [system_prompt]

for msg in history:
role = HumanMessage if msg['role'] == 'user' else AIMessage
langchain_messages.append(role(content=msg['content']))

langchain_messages.append(HumanMessage(content=message))
response = llm.invoke(langchain_messages)
return response.content

if __name__ == "__main__":
demo = gr.ChatInterface(fn=simple_chat, type="messages")
demo.launch()
15 changes: 15 additions & 0 deletions 05_src/assignment_chat/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# My AI Chatbot - Assignment 1

## What is this?
This is a web-based chat app I built using **Gradio** and **LangChain**. It’s hooked up to a GPT-4o-mini model running through an Amazon API Gateway. I’ve tuned the assistant to be witty and helpful, but it has some "no-go" zones to keep the conversation on track.

## What it can do
* **Natural Conversations:** It handles back-and-forth chatting and actually remembers what we talked about earlier in the session.
* **Strict Guardrails:** I’ve programmed the bot to dodge specific topics. It will politely (or wittily) refuse to talk about Pets, Zodiac signs, or Taylor Swift.
* **Secure Connection:** It connects to a cloud-based API Gateway using custom headers to keep the API key safe.

## Behind the Scenes (My Decisions)
1. **Finding the Secrets:** Pathing can be a headache in Python, so I used an absolute path to the `.secrets` file. This ensures the app finds the API key no matter which folder you launch it from.
2. **Mapping Messages:** Gradio and LangChain speak different "languages" when it comes to chat history. I wrote a loop to translate Gradio’s history into `HumanMessage` and `AIMessage` objects so the bot doesn't lose its train of thought.
3. **Keeping it Standard:** I stuck strictly to the libraries provided in the course (LangChain, Gradio, and Dotenv). No extra "fluff" or outside packages were used, making it easy to run on any standard setup.
4. **Prompt-Based Control:** Instead of writing a bunch of complex "if/else" code to block topics, I used a `SystemMessage`. This sets the bot's "personality" and rules right at the source, making the rejections feel more natural and less like a computer error.