Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created February 23, 2024 13:37
Show Gist options
  • Save bjoerntx/92338a79f5f0d468c8b8947213a5c17b to your computer and use it in GitHub Desktop.
Save bjoerntx/92338a79f5f0d468c8b8947213a5c17b to your computer and use it in GitHub Desktop.
public static List<string> GetKeywords(string text, int numKeywords = 10)
{
// create a list to store the keywords
List<string> keywords = new List<string>();
string prompt = $"Create {numKeywords} keywords and synonyms from the following question that can be used to find information in a larger text. Create only 1 word per keyword. Return the keywords in lowercase only. Here is the question: {text}";
// create a request object
Request apiRequest = new Request
{
Messages = new[]
{
new RequestMessage
{
Role = "system",
Content = $"Always provide {numKeywords} keywords that include relevant synonyms of words in the original question."
},
new RequestMessage
{
Role = "user",
Content = prompt
}
},
Functions = new[]
{
new Function
{
Name = "get_keywords",
Description = "Use this function to give the user a list of keywords.",
Parameters = new Parameters
{
Type = "object",
Properties = new Properties
{
List = new ListProperty
{
Type = "array",
Items = new Items
{
Type = "string",
Description = "A keyword"
},
Description = "A list of keywords"
}
}
},
Required = new List<string> { "list" }
}
},
FunctionCall = new FunctionCall
{
Name = "get_keywords",
Arguments = "{'list'}"
}
};
// get the response
if (GetResponse(apiRequest) is Response response)
{
// return the keywords
return System.Text.Json.JsonSerializer.Deserialize<ListReturnObject>(response.Choices[0].Message.FunctionCall.Arguments).List;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment