Загрузка...

Building AI chatbot

Build an AI chatbot using gemini AI
#chatbot.py
import os
from google import genai
from dotenv import load_dotenv

load_dotenv()

api_key = os.environ.get("GEMINI_API_KEY")
#print(api_key)

if not api_key:
print("ERROR: 'GEMINI_API_KEY' not found. Please check your .env file.")
exit()

def run_chatbot():
print("--- Gemini Chatbot Initialized ---")
print("Model: gemini-2.5-flash")
print("Ask me anything! Type 'exit' to end the conversation.")
print("-" * 35)

try:

client = genai.Client()
model_name = "gemini-2.5-flash"

except Exception as e:
print(f"Failed to initialize the Gemini client. Error: {e}")
return

while True:

user_question = input("Ask: ")
if user_question.lower() in ['exit', 'quit']:
print("Chatbot: Goodbye!")
break
if not user_question:
continue
try:

response = client.models.generate_content(

model=f"models/{model_name}",

contents=[user_question]
)
if response and response.candidates:

answer = response.candidates[0].content.parts[0].text
print(f"Gemini: {answer}") # Print the extracted answer.
else:

print("Gemini: I am unable to provide a response at this time.")

except Exception as e:
print(f"An error occurred during Gemini AI analysis: {str(e)}")

break
if __name__ == "__main__":

run_chatbot()

Видео Building AI chatbot канала SQL Depository
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять