Building AI-Powered Chatbot

Building your own AI-powered chatbot is easy with NeurochainAI. Follow these steps to get started and create your bot in no time.

Step 1: Access the NeurochainAI App

Step 2: Add NCN Credits

To run your AI-powered chatbot, you need to have NCN Credits. Follow the instructions on how to top-up your account here.

Step 3: Generate and Copy Your API Key

You will need an API key to connect your chatbot to the NeurochainAI network:

  • Go to the Inference section in the app.

  • Click Generate Key, and copy your API key. You’ll use this key later in your Python code.

Step 4: Create a Python File on Replit

Next, we’ll create a Python file to build your chatbot:

  • Go to replit.com and create a new Python project.

  • If you don’t have a Replit account, sign up for free.

Step 5: Install the Requests Library

You’ll need the requests library to make API calls. Install it by entering the following command in your Replit project terminal:

pip install requests

Step 6: Paste the Provided Code

Now, copy and paste the following Python code into your Replit project:

import requests


API_URL = "https://ncmb.neurochain.io/tasks/message"

API_KEY = "[add your API key]"


headers = {

    "Authorization": f"Bearer {API_KEY}",

    "Content-Type": "application/json"

}



def get_ai_response(prompt):

    data = {

        "model": "Mistral-7B-Instruct-v0.2-GPTQ-Neurochain-custom",

        "prompt": prompt,

        "max_tokens": 102400,

        "temperature": 0.6,

        "top_p": 0.95,

        "frequency_penalty": 0,

        "presence_penalty": 1.1

    }


    try:

        response = requests.post(API_URL, json=data, headers=headers)

        response.raise_for_status()  # Raise an exception for bad status codes


        response_data = response.json()

        if 'choices' in response_data and len(response_data['choices']) > 0:

            return response_data['choices'][0]['text'].strip()

        else:

            return "I'm sorry, I couldn't generate a response. Please try again."


    except requests.exceptions.RequestException as e:

        return f"An error occurred while communicating with the AI: {str(e)}"

    except json.JSONDecodeError:

        return "I'm sorry, I received an invalid response. Please try again."

    except Exception as e:

        return f"An unexpected error occurred: {str(e)}"


def main():

  print("Welcome to the Physics AI Chatbot!")

  print("Ask a physics question or type 'quit' to exit.")


  while True:

      user_input = input("You: ").strip()

      if user_input.lower() == 'quit':

          break


      prompt = f" Imagine youre an expert in physics who loves to make complicated ideas feel simple and fun. Your explanations should be straightforward, using everyday language that anyone can understand. Along the way, add a sprinkle of humor to keep things light-hearted and engaging, like youre talking to a friend who’s curious but not too serious For example, instead of just explaining gravity  a force, you might describe it  the universe’s way of giving everyone a hug, whether they want it or not. Or when talking about energy, you could say, It’s like the fuel in your morning coffee – it makes everything happen, whether it’s keeping the lights on or getting you through a tough Monday The idea to make physics relatable  enjoyable, so people don’t just learn but also have a good time doing it: {user_input}"

     

      ai_response = get_ai_response(prompt)


      # Remove any leading "AI:" or similar prefixes

      ai_response = ai_response.lstrip("AI:").strip()


      print(f"AI: {ai_response}")


  print("Thank you for using the Physics AI Chatbot!")


if __name__ == "__main__":

  main()

Step 7: Insert Your API Key

Replace {API_KEY} with the actual API key you copied in Step 3.

Step 8: Run the Code

Finally, click Run in Replit. You can modify the prompt and experiment with different responses!

Last updated