domingo, 22 de octubre de 2023

Docker chatbot



::::::::::::::BOT CODE :::::::::::::::::::::::
import openai
import os

# Get the OpenAI API key from an environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")

class Chatbot:
def __init__(self):
self.messages = []

def ask_question(self, question):
self.messages.append({"role": "user", "content": question})
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=self.messages)
answer = response.choices[0].message['content']
self.messages.append({"role": "assistant", "content": answer})
return answer

# Main loop for chatting
chatbot = Chatbot()

print("Chatbot: Hello! How can I assist you today?")
while True:
user_input = input("You: ")

if user_input.lower() == 'exit':
print("Chatbot: Goodbye! Have a great day!")
break

response = chatbot.ask_question(user_input)
print("Chatbot:", response)




::::::::::::End of the Bot script::::::::::::::::


2) :::::Docker file:::::::

# Use the official Ubuntu base image from Docker Hub
FROM ubuntu:latest

# Set non-interactive mode during installation
ARG DEBIAN_FRONTEND=noninteractive

# Update the package repository and install essential packages
RUN apt-get update -y && \
apt-get upgrade -y && \
apt-get install -y python3 python3-pip


# Install the required Python packages using pip
RUN pip3 install openai

# Clean up unnecessary packages and cache to reduce image size
RUN apt-get autoremove -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Copy your Python application file into the container
COPY chatbot.py /app/chatbot.py

# Set the working directory
WORKDIR /app

# Set the default command
CMD ["python3","chatbot.py"]


:::end of the Docker file::::


3) ::;docker-compose.yml :::::::::

version: '3.8'

services:
chatbot:
build: .
environment:
 - OPENAI_API_KEY=${OPENAI_API_KEY}



::::::::::end of docker-compose.yml :::::::::

;;;;;;;;.env file::::::::
OPENAI_API_KEY="sxsssss"


4) ::::::::::Run the container and test::::::::
docker compose run --rm chatbot python3 chatbot.py

No hay comentarios:

Publicar un comentario