Einzelnen Beitrag anzeigen

API

Registriert seit: 18. Apr 2004
637 Beiträge
 
#23

AW: Programme bzw. Funktionen von KI schreiben lassen (OpenAI)

  Alt 16. Dez 2022, 09:26
Me: Create a Delphi console application that implements a chatbot

Chatbot: Sure! Here is an example of how you can create a simple chatbot in Delphi using the console application:

Delphi-Quellcode:
program Chatbot;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  input: string;

begin
  WriteLn('Hello, I am a chatbot. How can I help you today?');

  while True do
  begin
    Write('You: ');
    ReadLn(input);

    if input = 'exitthen
      Break;

    case input of
      'hi', 'hello':
        WriteLn('Chatbot: Hi there! How are you doing?');
      'how are you':
        WriteLn('Chatbot: I am just a computer program, so I don''t have feelings. But I am here to assist you.');
      'what can you do':
        WriteLn('Chatbot: I can answer questions, provide information, and carry out simple tasks. Is there anything specific you would like to know or do?');
      else
        WriteLn('Chatbot: I am sorry, I didn''t understand your question. Could you please rephrase it or ask for something else?');
    end;
  end;
end.
This chatbot will greet the user and then enter into a loop where it will continually read the user's input and respond with a predefined message. The chatbot can understand and respond to a few specific phrases, such as "hi", "hello", "how are you", and "what can you do". If the user enters any other phrase, the chatbot will respond with a default message.

To exit the chatbot, the user can type "exit".

I hope this helps! Let me know if you have any questions or need further assistance.

Me: Now let's add a functionality to the chatbot so it will be able to do some simple math operations.


Chatbot:

Sure! Here is an example of how you can modify the chatbot to perform simple math operations:

Delphi-Quellcode:
program Chatbot;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

var
  input: string;
  num1, num2: Integer;
  result: Real;

begin
  WriteLn('Hello, I am a chatbot. How can I help you today?');

  while True do
  begin
    Write('You: ');
    ReadLn(input);

    if input = 'exitthen
      Break;

    case input of
      'hi', 'hello':
        WriteLn('Chatbot: Hi there! How are you doing?');
      'how are you':
        WriteLn('Chatbot: I am just a computer program, so I don''t have feelings. But I am here to assist you.');
      'what can you do':
        WriteLn('Chatbot: I can answer questions, provide information, and carry out simple tasks. Is there anything specific you would like to know or do?');
      'add', 'sum':
        begin
          Write('Enter the first number: ');
          ReadLn(num1);
          Write('Enter the second number: ');
          ReadLn(num2);
          result := num1 + num2;
          WriteLn('Chatbot: The sum of the two numbers is ', result);
        end;
      'subtract', 'difference':
        begin
          Write('Enter the first number: ');
          ReadLn(num1);
          Write('Enter the second number: ');
          ReadLn(num2);
          result := num1 - num2;
          WriteLn('Chatbot: The difference of the two numbers is ', result);
        end;
      'multiply', 'product':
        begin
          Write('Enter the first number: ');
          ReadLn(num1);
          Write('Enter the second number: ');
          ReadLn(num2);
          result := num1 * num2;
          WriteLn('Chatbot: The product of the two numbers is ', result);
        end;
      'divide', 'quotient':
        begin
          Write('Enter the first number: ');
          ReadLn(num1);
          Write('Enter the second number: ');
          ReadLn(num2);
          if num2 = 0 then
            WriteLn('Chatbot: Cannot divide by zero!')
          else
          begin
            result := num1 / num2;
            WriteLn('Chatbot: The quotient of the two numbers is ', result);
          end;
        end;
      else
        WriteLn('Chatbot: I am sorry, I didn''t understand your question. Could you please rephrase it or ask for something else?');
    end;
  end;
end
  Mit Zitat antworten Zitat