Coder Social home page Coder Social logo

marcel2215 / functional-gpt Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 96 KB

Lightweight C#/.NET OpenAI API wrapper with support of GPT function calling via reflection. New repository: https://github.com/chataize/generative-cs

Home Page: https://github.com/chataize/generative-cs

License: MIT License

C# 100.00%
api chatgpt dotnet function-calling gpt gpt-3 gpt-4 library openai wrapper

functional-gpt's Introduction

Caution

This project is now deprecated and being replaced by GenerativeCS.




hero-image

example-screenshot

FunctionalGPT (.NET 7.0+)

Lightweight C#/.NET OpenAI API wrapper with support of GPT function calling via reflection.

Usage

Simple Prompt

using FunctionalGPT;

var chatGPT = new ChatGPT("<OPENAI API KEY>", "gpt-3.5-turbo");
var response = chatGPT.CompleteAsync("Write an article about Bitcoin.");

Console.WriteLine(response);

Conversation

using FunctionalGPT;

var chatGPT = new ChatGPT("<OPENAI API KEY>", "gpt-3.5-turbo");
var conversation = new Conversation("You are a helpful assistant [...]");

while (true)
{
    var userMessage = Console.ReadLine()!;
    conversation.FromUser(userMessage);

    var assistantResponse = await chatGPT.CompleteAsync(conversation);
    Console.WriteLine(assistantResponse);
}

Important

Assistant replies are automatically added to the conversation.

Conversation with Multiple Users

using FunctionalGPT;

var chatGPT = new ChatGPT("<OPENAI API KEY>", "gpt-3.5-turbo");
var conversation = new Conversation("Decide who is right and explain why.");

conversation.FromUser("Alice", "I think the egg came first.");
conversation.FromUser("Bob", "Nah, it must have been chicken.");

var response = await chatGPT.CompleteAsync(conversation);
Console.WriteLine(response);

Function Calling

Warning

Top-level and anonymous functions are currently not supported.

using FunctionalGPT;
using System.ComponentModel;

var chatGPT = new ChatGPT("<OPENAI API KEY>", "gpt-3.5-turbo");

chatGPT.AddFunction(SmartHome.LockDoor);
chatGPT.AddFunction(SmartHome.ChangeTemperature);

var conversation = new Conversation("You are a helpful assistant [...]");

while (true)
{
    var userMessage = Console.ReadLine()!;
    conversation.FromUser(userMessage);

    var assistantResponse = await chatGPT.CompleteAsync(conversation);
    Console.WriteLine(assistantResponse);
}

public static class SmartHome
{
    [Description("Here you can describe the function (optional).")]
    public static void LockDoor(bool isLocked)
    {
        // Perform some logic to lock/unlock the door:
        Console.WriteLine($"[SMART HOME] Changed door lock status to: {isLocked}.");

        // Functions don't have to return anything.
    }

    // Function parameters can have default values specified:
    public static async Task<string> ChangeTemperature(int newTemperature = 20)
    {
        // Async functions are also supported:
        await Task.Delay(5000);

        // Validate arguments like this:
        if (newTemperature is < 0 or > 30)
        {
            // Functions can respond directly to the model:
            return "ERROR! Temperature must be between 0 and 30.";
        }

        // Perform some logic to change the temperature:
        Console.WriteLine($"[SMART HOME] Changed temperature to: {newTemperature}.");

        return "SUCCESS!";
    }
}

Enums, Collections and Objects

using FunctionalGPT;

var chatGPT = new ChatGPT("<OPENAI API KEY>", "gpt-3.5-turbo");

chatGPT.AddFunction(Restaurant.GetAvailableTypes);
chatGPT.AddFunction(Restaurant.CreateOrder);
chatGPT.AddFunction(Restaurant.CancelOrder);

var conversation = new Conversation("You work in a pizzeria, and your goal is to collect orders.");

while (true)
{
    var userMessage = Console.ReadLine()!;
    conversation.FromUser(userMessage);

    var assistantResponse = await chatGPT.CompleteAsync(conversation);
    Console.WriteLine(assistantResponse);
}

public enum PizzaSize
{
    Small,
    Medium,
    Large
}

public record Pizza(string Type, PizzaSize Size, int Quantity);

public static class Restaurant
{
    private static readonly string[] _availableTypes =
    {
        "margherita",
        "pepperoni",
        "supreme",
        "vegetarian",
        "hawaiian"
    };

    public static string[] GetAvailableTypes()
    {
        return _availableTypes;
    }

    public static object CreateOrder(List<Pizza> pizzas)
    {
        // Validate the order:
        foreach (var pizza in pizzas)
        {
            if (!_availableTypes.Contains(pizza.Type.ToLower()))
            {
                return "ERROR! We don't serve this type of pizza.";
            }
        }

        // Perform some logic to save the order:
        foreach (var pizza in pizzas)
        {
            Console.WriteLine($"{pizza.Quantity}x {pizza.Size} {pizza.Type}");
        }

        // You can return anything serializable by System.Text.Json:
        return new { Success = true, OrderId = Random.Shared.Next() };
    }

    public static void CancelOrder(int id)
    {
        // Perform some logic to cancel the order.
        // WARNING: Make sure that the user can only cancel their own orders.

        Console.WriteLine($"Canceled order: {id}");
    }
}

functional-gpt's People

Contributors

marcel2215 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.