Skip to content

Commit

Permalink
docs: add documentation to everything publically accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
JKamue committed Jun 10, 2024
1 parent 40897fc commit 9fa5177
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 1 deletion.
22 changes: 22 additions & 0 deletions EasyOpenAiTools.Library/OpenAi/OpenAiChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

namespace EasyOpenAiTools.Library.OpenAi
{
/// <summary>
/// The <c>OpenAiChat</c> class facilitates interaction with an OpenAI language model by maintaining a chat session.
/// It allows sending messages to the model and receiving responses.
/// </summary>
public class OpenAiChat
{
private readonly OpenAiModel _model;
Expand All @@ -19,6 +23,15 @@ private SystemChatMessage CreateInitialMessage(string initialPrompt)
return new SystemChatMessage(initialPrompt);
}

/// <summary>
/// Sends a message to the OpenAI model and returns the response as a string.
/// </summary>
/// <param name="message">The message to send to the model.</param>
/// <returns>The response from the model as a string. If the response fails, a failure message is contained in the returned string.</returns>
/// <remarks>
/// Check out <see cref="AskWithResult(string)"/> that wont embed errors into the resulting string and will instead return
/// a result monade that is either a <c>Success</c> or <c>Failure</c>
/// </remarks>
public async Task<string> Ask(string message)
{
var messageResult = await AskWithResult(message);
Expand All @@ -29,6 +42,11 @@ public async Task<string> Ask(string message)
return messageResult.Value;
}

/// <summary>
/// Sends a message to the OpenAI model and returns the result as a <see cref="Result{string}"/> object.
/// </summary>
/// <param name="message">The message to send to the model.</param>
/// <returns>A <see cref="Result{string}"/> object containing the response from the model. If the response fails, the result contains the error.</returns>
public async Task<Result<string>> AskWithResult(string message)
{
var messageResult = await _model.Ask(message, messageLog);
Expand All @@ -40,6 +58,10 @@ public async Task<Result<string>> AskWithResult(string message)
return GetLastMessage();
}

/// <summary>
/// Gets the content of the last message in the chat.
/// </summary>
/// <returns>The content of the last message as a string.</returns>
public string GetLastMessage() => messageLog.Last().Content.First().Text;
}
}
9 changes: 9 additions & 0 deletions EasyOpenAiTools.Library/OpenAi/OpenAiChatFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

namespace EasyOpenAiTools.Library.OpenAi
{
/// <summary>
/// The <c>OpenAiChatFactory</c> class provides a factory method for starting new <see cref="OpenAiChat"/>s.
/// </summary>
public class OpenAiChatFactory
{
/// <summary>
/// Starts a new chat session with the specified OpenAI settings.
/// </summary>
/// <param name="settings">The OpenAI settings to use for the chat.</param>
/// <param name="logger">Optional logger to be used for logging chat activity. If not provided, logging will be disabled.</param>
/// <returns>An instance of <see cref="OpenAiChat"/> initialized with the provided settings and ready to be used for asking questions.</returns>
public static OpenAiChat StartNewChat(OpenAiSettings settings, ILogger? logger = null)
{
var model = new OpenAiModel(settings, logger);
Expand Down
17 changes: 16 additions & 1 deletion EasyOpenAiTools.Library/OpenAi/OpenAiModelType.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
namespace EasyOpenAiTools.Library.OpenAi
{
/// <summary>
/// The <c>OpenAiModelType</c> class represents different types of OpenAI language models.
/// </summary>
public class OpenAiModelType
{
/// <summary>
/// Gets the GPT-4 model type.
/// </summary>
public static OpenAiModelType Gpt4 => new OpenAiModelType("gpt-4o");

/// <summary>
/// Gets the GPT-3.5 model type (Turbo variant).
/// </summary>
public static OpenAiModelType Gpt35 => new OpenAiModelType("gpt-3.5-turbo-0125");

public string Name { get; init; }
internal string Name { get; init; }

private OpenAiModelType(string name)
{
Expand All @@ -14,6 +24,11 @@ private OpenAiModelType(string name)

public override string ToString() => Name;

/// <summary>
/// Implicitly converts an <see cref="OpenAiModelType"/> instance to its string representation.
/// </summary>
/// <param name="model">The <see cref="OpenAiModelType"/> instance to convert.</param>
/// <returns>The name of the model type.</returns>
public static implicit operator string(OpenAiModelType model) => model.ToString();
}
}
15 changes: 15 additions & 0 deletions EasyOpenAiTools.Library/OpenAi/OpenAiSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
namespace EasyOpenAiTools.Library.OpenAi;

/// <summary>
/// The <c>OpenAiSettings</c> record represents the settings required to configure an OpenAI chat.
/// </summary>
public record OpenAiSettings(
/// <summary>
/// The OpenAI API key used to authenticate requests.
/// </summary>
string OpenAiApiKey,

/// <summary>
/// The type of OpenAI language model to use for the chat session.
/// </summary>
OpenAiModelType OpenAiModel,

/// <summary>
/// The initial prompt to start the chat session with.
/// </summary>
string InitialPrompt
);
28 changes: 28 additions & 0 deletions EasyOpenAiTools.Library/Tool/Attributes/ToolAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
namespace EasyOpenAiTools.Library.Tool.Attributes
{
/// <summary>
/// The <c>ToolAttribute</c> class is a custom attribute used to mark a class as a tool.
/// This facilitates the automatic discovery and registration of tools, allowing them to be loaded
/// automatically when creating an <c>OpenAiChat</c>.
/// </summary>
/// <remarks>
/// For a class to be a valid tool it needs exactly one function of any name to be marked with the
/// <see cref="ToolMethodAttribute"/>. If your tool needs parameters add public properties and mark
/// them with the <see cref="ToolPropertyAttribute"/>. A tool does not need any properties. An example
/// for a tool with no properties would be a tool returning the current time.
/// </remarks>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class ToolAttribute : Attribute
{
/// <summary>
/// Returns the name of the tool. This is used by the LLM to choose tools and is also included
/// in logs if logging is activated. It is recommended to choose a representative, recognizable name.
/// </summary>
public string Name { get; }

/// <summary>
/// Returns the description of the tool. The LLM uses this description when choosing a tool.
/// Ensure that the description reflects the uses of the tool and maybe also mentions what
/// the tool cannot be used for.
/// </summary>
public string Description { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ToolAttribute"/> class with the specified name and description.
/// The following information is used by the LLM when selecting a tool. Use them to outline what your tool is
/// capable of and, if needed, also mention what the tool cannot be used for.
/// </summary>
/// <param name="name">The name of the tool. Used by the LLM to decide which tool to use.</param>
/// <param name="description">The description of the tool. Used by the LLM to decide which tool to use.</param>
public ToolAttribute(string name, string description)
{
Name = name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
namespace EasyOpenAiTools.Library.Tool.Attributes
{
/// <summary>
/// The <c>ToolMethodAttribute</c> class is a custom attribute used to mark the method of a tool.
/// Each tool must mark one and only one method with the <c>ToolMethodAttribute</c> attribute.
/// This attribute is used internally to identify which methods are executable by the LLM when a certain tool is called.
/// The name of the method to which this attribute is applied is not communicated to the LLM and can be chosen freely by the developer.
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class ToolMethodAttribute : Attribute
{
Expand Down
21 changes: 21 additions & 0 deletions EasyOpenAiTools.Library/Tool/Attributes/ToolPropertyAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
namespace EasyOpenAiTools.Library.Tool.Attributes
{
/// <summary>
/// The <c>ToolPropertyAttribute</c> class is a custom attribute used to mark a property as an argument of a Tool.
/// This allows the LLM to understand which arguments it needs to ask for from the user and also tells it how
/// to use the property, including any necessary formats or ranges.
/// </summary>
/// <remarks>
/// This attribute can be applied to properties multiple times and is intended for properties with either an <c>init</c> or <c>set</c> accessor.
/// Only properties of the type <c>string</c> are supported.
/// </remarks>
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class ToolPropertyAttribute : Attribute
{
/// <summary>
/// Gets the name of the property.
/// </summary>
public string Name { get; }

/// <summary>
/// Gets the description of the property. The description should include any necessary formats, ranges, or other constraints.
/// </summary>
public string Description { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ToolPropertyAttribute"/> class with the specified name and description.
/// </summary>
/// <param name="name">The name of the property.</param>
/// <param name="description">The description of the property, including guides on usage as well as necessary formats and ranges.</param>
public ToolPropertyAttribute(string name, string description)
{
Name = name;
Expand Down

0 comments on commit 9fa5177

Please sign in to comment.