-
Notifications
You must be signed in to change notification settings - Fork 21
Extend .WithSource to Support APIs Requiring API Keys #76
Copy link
Copy link
Labels
enhancementNew feature or requestNew feature or request
Milestone
Description
Extend .WithSource to Support APIs Requiring API Keys
This feature extends the existing .WithSource functionality to support integration with external APIs that require authentication via API keys. It enables developers to define custom API interactions and inject the response as context for LLM processing.
Implementation Overview
- Allows detailed configuration of API call parameters through
AgentApiSourceDetails, including:- Authentication via
AuthorisationTokenpassed as a header. - Flexible request definition, either by:
- Supplying a raw
curlcommand string. - Or optionally providing a structured
Payloadobject (JSON).
- Supplying a raw
- Authentication via
- API responses are fetched and used as context alongside the LLM prompt.
- No assumptions are made about the structure of the request payload (e.g.,
"q"vs."question"), ensuring compatibility with various external services. - Added parsing for double-serialized JSON in text passed to the model, improving performance for smaller LLMs.
Example Usage
var context = AIHub.Agent()
.WithModel("gemma2-2b")
.WithInitialPrompt("Provide info on request recieid")
.WithSource(new AgentApiSourceDetails()
{
Method = "Post",
Url = "https://api.tavily.com/search",
ResponseType = "JSON",
ChunkLimit = 10,
AuthorisationToken = token,
Curl = @"curl --request POST \
--url https://api.tavily.com/search \
--header 'Authorization: Bearer tavily-dev-addwda' \
--header 'Content-Type: application/json' \
--data '{
""query"": ""who is Leo Messi?""
}'"
}, AgentSourceType.API)
.WithSteps(StepBuilder.Instance.FetchData().Build())
.Create();
var result = await context
.ProcessAsync("Summarise me this");
Authorization Header Override
Note:
If both theAuthorisationTokenproperty and anAuthorizationheader are specified in theCurlcommand string,
the value provided inAuthorisationTokenwill override theAuthorizationheader in theCurlstring.This ensures that the API key or token set via the
AuthorisationTokenproperty is always used for authentication.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request