Getting Started with IPWorks LLM


Requirements:
IPWorks LLM

Introduction

The LLMClient component in IPWorks LLM makes it easy to send messages to language models, maintain conversation history, and stream responses. This guide covers generating a response, continuing a conversation, switching providers, and streaming replies.

Tip: Download the latest version of IPWorks LLM to follow along.

Step 1: Generate a First Response

Set ServiceProvider, APIKey, and Model, then add a user message and call Generate. This example uses OpenAI:


using nsoftware.IPWorksLLM;
LLMClient llmclient = new LLMClient();
llmclient.ServiceProvider = LLMClientServiceProviders.spOpenAI;
llmclient.APIKey = "api-key";
llmclient.Model = "gpt-4o-mini";
llmclient.AddMessage("user", "What is the capital of France?");
llmclient.Generate();
Console.WriteLine(llmclient.ResponseText);

The sample key is a placeholder; use a key for your account and a model available to that account. If you need to check the model identifier, call ListModels.

Generate stores the response in ResponseText and adds it to the conversation history in Messages and MessageParts.

Step 2: Continue or Reset the Conversation

Add another user message and call Generate again:


llmclient.AddMessage("user", "What language is primarily spoken there?");
llmclient.Generate();
Console.WriteLine(llmclient.ResponseText);

The component sends the first exchange along with the follow-up, so the model can understand "there" as referring to France. You only need to add the new message.

When you want to start a new conversation, call Reset:


llmclient.Reset();

This clears messages, parts, tools, and the latest reply. The provider, API key, and model remain set.

Step 3: Switch Services

You can use the same LLMClient to send the next turn to another provider. For Anthropic, change ServiceProvider, APIKey, and Model. It also requires a positive MaxOutputTokens value:


llmclient.ServiceProvider = LLMClientServiceProviders.spAnthropic;
llmclient.APIKey = "anthropic-api-key";
llmclient.Model = "anthropic-model";
llmclient.ServerURL = "";
llmclient.MaxOutputTokens = 1024;

Gemini needs its own provider value, API key, and model:


llmclient.ServiceProvider = LLMClientServiceProviders.spGemini;
llmclient.APIKey = "gemini-api-key";
llmclient.Model = "gemini-model";
llmclient.ServerURL = "";
llmclient.MaxOutputTokens = 0;

Note: Changing ServiceProvider keeps the conversation and leaves APIKey, Model, and ServerURL unchanged. Clear ServerURL, as in the examples, to use the new provider's default endpoint. Review the conversation before sending it to another service.

MaxOutputTokens also carries over. Set it to 0 to use the new provider's default limit; Anthropic requires a positive value.

Local and Custom Services

For a local Ollama server, select spOllama and leave ServerURL empty to use its default endpoint. No API key is required:


llmclient.ServiceProvider = LLMClientServiceProviders.spOllama;
llmclient.APIKey = "";
llmclient.ServerURL = "";
llmclient.Model = "local-model";

The default Ollama endpoint is http://localhost:11434/v1. Replace local-model with the name of a model installed on that server. If Ollama runs on another host or port, set ServerURL to its base URL instead.

For a service without a built-in provider value, select the matching custom API format and provide its base URL:


llmclient.ServiceProvider = LLMClientServiceProviders.spCustomOpenAI;
llmclient.ServerURL = "http://192.168.1.20:8000/v1";
llmclient.Model = "meta-llama/Llama-3.1-8B-Instruct";

Unlike the built-in providers, custom values have no default endpoint, so ServerURL is required. Use the API base URL rather than the full chat completions route. APIKey retains its previous value when you switch providers, so update or clear it for the new service.

For local setup, see Using IPWorks LLM with Ollama or Using IPWorks LLM with LM Studio.

Step 4: Stream a Reply

To stream a response, set Stream to true and handle the ContentDelta event. Since the event can contain different types of content, check PartType before printing text:


llmclient.Reset();
llmclient.ServiceProvider = LLMClientServiceProviders.spOpenAI;
llmclient.APIKey = "api-key";
llmclient.Model = "gpt-4o-mini";
llmclient.ServerURL = "";
llmclient.MaxOutputTokens = 0;
llmclient.Stream = true;
llmclient.OnContentDelta += (s, e) =>
{
  if (e.PartType == (int)PartTypes.ptText)
    Console.Write(e.Delta);
};
llmclient.AddMessage("user", "Write a short poem about the sea.");
llmclient.Generate();
Console.WriteLine();

Generate still assembles the complete reply in ResponseText, even though ContentDelta has already displayed it piece by piece.

Troubleshooting

If a request fails or returns no text, these checks can help narrow down the cause:

  • Authentication fails: Check that APIKey belongs to the selected service and that the account can use the chosen model.
  • The model is not found: Check the identifier in Model against a name returned by ListModels. For a local server, confirm that the model is installed or loaded.
  • The request goes to the wrong server: Clear ServerURL to use a built-in provider's default endpoint. Custom provider values require a base URL; local servers must be running at the address you specify.
  • The reply text is empty: ResponseText contains only text. Check MessageParts for other content and FinishReason to determine why generation stopped. A provider may block a request without returning an error or an assistant message.

Conclusion

For a closer look at service-specific settings and capabilities, see IPWorks LLM Provider Comparison. Managing IPWorks LLM Conversations explains how to save and resume a conversation, while Tool Calling with IPWorks LLM covers calls to application tools.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.