← All posts

By frank.bailey.jr ·August 21, 2026 · updated August 21, 2026· 2 min read

Task ๐Ÿฎ of KodeKloud's ๐—”๐—œ - ๐—Ÿ๐—ฒ๐˜ƒ๐—ฒ๐—น ๐Ÿญ curriculum. Today's task was to Create an AI Chatbot.

devopsmlopsaillmskodekloud

task_2_code

Here were the task requirements: โ€ข Create a client instance using api_key and base_url โ€ข Define a variable prompt with the following content: You are a friendly travel guide. Greet the user and ask where they want to go. โ€ข Send this prompt to the OpenAI chat model and store the result in variable named ๐˜ณ๐˜ฆ๐˜ด๐˜ฑ๐˜ฐ๐˜ฏ๐˜ด๐˜ฆ โ€ข Define a variable prompt with the following content: model: openai/gpt-4.1-mini temperature: 0.7 max_tokens: 100

Please review the script example provided. Here are some things to pay attention to:

  1. I added comments on each section for clarification on what is happening and matching up with the task requirements
  2. I didn't hard-code ANY api keys or base_urls. This is for 2 reasons, security and to make this modularized. This allows ANYONE to run this script with their own OpenAI credentials
  3. I used the OpenAI library, not LangChain. This was due to the task requirements
  4. To set up the local venv environment, I ran the following: ๐š™๐šข๐š๐š‘๐š˜๐š—๐Ÿน -๐š– ๐šŸ๐šŽ๐š—๐šŸ ๐šŸ๐šŽ๐š—๐šŸ && ๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ ๐šŸ๐šŽ๐š—๐šŸ/๐š‹๐š’๐š—/๐šŠ๐šŒ๐š๐š’๐šŸ๐šŠ๐š๐šŽ && ๐š™๐š’๐š™ ๐š’๐š—๐šœ๐š๐šŠ๐š•๐š• ๐š˜๐š™๐šŽ๐š—๐šŠ๐š’
import os

# declaring the OpenAI client and passing explicitly the api_key and base_url
# the values are passed from the ENV keys found in /root/.bash_profile
client = OpenAI(
    base_url=os.environ.get("OPENAI_API_BASE"),
    api_key=os.environ.get("OPENAI_API_KEY")
)

prompt="You are a friendly travel guide. Greet the user and ask where they want to go."


# defining greet_user function with a user_prompt parameter for user prompt
def greet_user(user_prompt: str) -> str:
    # declaring model, max_tokens, and temperature
    response = client.chat.completions.create(
        model="openai/gpt-4.1-mini",
        messages=[
            {
                "role": "system", 
                "content": prompt
            },
            {
                "role": "user", 
                "content": user_prompt
            }
        ],
        temperature=0.7,
        max_tokens=100
    )
    # Extract and return the response text
    return response.choices[0].message.content

# declaring main
def main():
    response = greet_user("Hello")
    if response:
        # print the response
        print(response)
    else:
        print("Failed to retrieve response from LLM")

if __name__ == "__main__":
    main()

I hope you found this informative! More to come!![task_2_code]