Skip to content

Commit

Permalink
added similar helpers for assistants
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina committed Jun 10, 2024
1 parent 79ed768 commit 5c30ea2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 23 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,7 @@ Next, create a new thread. For illustrative purposes, you could include an initi
```csharp
ThreadCreationOptions threadOptions = new()
{
InitialMessages =
{
new ThreadInitializationMessage(new List<MessageContent>()
{
MessageContent.FromText("How well did product 113045 sell in February? Graph its trend over time."),
}),
},
InitialMessages = { "How well did product 113045 sell in February? Graph its trend over time." }
};

ThreadRun threadRun = assistantClient.CreateThreadAndRun(assistant.Id, threadOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,7 @@ public void Example01_RetrievalAugmentedGeneration()
// Now we'll create a thread with a user query about the data already associated with the assistant, then run it
ThreadCreationOptions threadOptions = new()
{
InitialMessages =
{
new ThreadInitializationMessage(new List<MessageContent>()
{
MessageContent.FromText("How well did product 113045 sell in February? Graph its trend over time."),
}),
},
InitialMessages = { "How well did product 113045 sell in February? Graph its trend over time." }
};

ThreadRun threadRun = assistantClient.CreateThreadAndRun(assistant.Id, threadOptions);
Expand Down
2 changes: 1 addition & 1 deletion examples/Assistants/Example02_FunctionCalling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ string GetCurrentWeather(string location, string unit = "celsius")
// Create a thread with an initial user message and run it.
ThreadCreationOptions threadOptions = new()
{
InitialMessages = { new ThreadInitializationMessage(["What's the weather like today?"]), },
InitialMessages = { "What's the weather like today?" }
};

ThreadRun run = client.CreateThreadAndRun(assistant.Id, threadOptions);
Expand Down
2 changes: 1 addition & 1 deletion examples/Assistants/Example02_FunctionCallingAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ string GetCurrentWeather(string location, string unit = "celsius")
// Create a thread with an initial user message and run it.
ThreadCreationOptions threadOptions = new()
{
InitialMessages = { new ThreadInitializationMessage(["What's the weather like today?"]), },
InitialMessages = { "What's the weather like today?" }
};

ThreadRun run = await client.CreateThreadAndRunAsync(assistant.Id, threadOptions);
Expand Down
11 changes: 4 additions & 7 deletions examples/Assistants/Example04_AllTheTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,10 @@ static string GetNameOfFamilyMember(string relation)
{
InitialMessages =
{
new ThreadInitializationMessage(
[
"Create a graph of a line with a slope that's my father's favorite number "
+ "and an offset that's my mother's favorite number.",
"Include people's names in your response and cite where you found them."
]),
},
"Create a graph of a line with a slope that's my father's favorite number "
+ "and an offset that's my mother's favorite number.",
"Include people's names in your response and cite where you found them."
}
});

ThreadRun run = client.CreateRun(thread, assistant);
Expand Down
11 changes: 11 additions & 0 deletions src/Custom/Assistants/ThreadInitializationMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ public ThreadInitializationMessage(IEnumerable<MessageContent> content) : base(c
internal ThreadInitializationMessage(MessageCreationOptions baseOptions)
: base(baseOptions.Role, baseOptions.Content, baseOptions.Attachments, baseOptions.Metadata, null)
{ }

/// <summary>
/// Implicitly creates a new instance of <see cref="ThreadInitializationMessage"/> from a single item of plain text
/// content.
/// </summary>
/// <remarks>
/// Using a <see cref="string"/> in the position of a <see cref="ThreadInitializationMessage"/> is equivalent to
/// using the <see cref="ThreadInitializationMessage(IEnumerable{MessageContent})"/> constructor with a single
/// <see cref="MessageContent.FromText(string)"/> content instance.
/// </remarks>
public static implicit operator ThreadInitializationMessage(string initializationMessage) => new([initializationMessage]);
}
10 changes: 10 additions & 0 deletions src/Custom/Chat/ChatMessageContentPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,14 @@ public static ChatMessageContentPart CreateImageMessageContentPart(BinaryData im
/// </summary>
/// <returns></returns>
public override string ToString() => Text;

/// <summary>
/// Implicitly creates a new <see cref="ChatMessageContentPart"/> instance from an item of plain text.
/// </summary>
/// <remarks>
/// Using a <see cref="string"/> in the position of a <see cref="ChatMessageContentPart"/> is equivalent to
/// calling the <see cref="CreateTextMessageContentPart(string)"/> method.
/// </remarks>
/// <param name="content"> The text content to use as this content part. </param>
public static implicit operator ChatMessageContentPart(string content) => new(content);
}

0 comments on commit 5c30ea2

Please sign in to comment.