From d2b92a45be8c22787c08b8e2995b6f5c03c812ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20S=C3=A1nchez-Gallego=20Kadri?= <126669056+laurasgkadri98@users.noreply.github.com> Date: Thu, 13 Jun 2024 12:29:56 +0200 Subject: [PATCH] feat: add ansys gpt functions (#26) --- go.mod | 3 + go.sum | 4 + pkg/externalfunctions/externalfunctions.go | 109 ++++++++++++++++++ .../externalfunctions_test.go | 88 ++++++++++++++ pkg/externalfunctions/privatefunctions.go | 15 +++ pkg/externalfunctions/types.go | 7 ++ 6 files changed, 226 insertions(+) create mode 100644 pkg/externalfunctions/externalfunctions_test.go diff --git a/go.mod b/go.mod index 80c1d2c..981c6e2 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,10 @@ require ( nhooyr.io/websocket v1.8.11 ) +require github.com/texttheater/golang-levenshtein v1.0.1 // indirect + require ( + github.com/schollz/closestmatch v2.1.0+incompatible golang.org/x/net v0.23.0 // indirect golang.org/x/sys v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/go.sum b/go.sum index ce05034..0228d48 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,10 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/schollz/closestmatch v2.1.0+incompatible h1:Uel2GXEpJqOWBrlyI+oY9LTiyyjYS17cCYRqP13/SHk= +github.com/schollz/closestmatch v2.1.0+incompatible/go.mod h1:RtP1ddjLong6gTkbtmuhtR2uUrrJOpYzYRvbcPAid+g= +github.com/texttheater/golang-levenshtein v1.0.1 h1:+cRNoVrfiwufQPhoMzB6N0Yf/Mqajr6t1lOv8GyGE2U= +github.com/texttheater/golang-levenshtein v1.0.1/go.mod h1:PYAKrbF5sAiq9wd+H82hs7gNaen0CplQ9uvm6+enD/8= golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= diff --git a/pkg/externalfunctions/externalfunctions.go b/pkg/externalfunctions/externalfunctions.go index 8ef2944..bc13118 100644 --- a/pkg/externalfunctions/externalfunctions.go +++ b/pkg/externalfunctions/externalfunctions.go @@ -7,8 +7,12 @@ import ( "io" "log" "net/http" + "sort" + "strings" "github.com/ansys/allie-flowkit/pkg/config" + "github.com/schollz/closestmatch" + "github.com/texttheater/golang-levenshtein/levenshtein" ) var ExternalFunctionsMap = map[string]interface{}{ @@ -30,6 +34,8 @@ var ExternalFunctionsMap = map[string]interface{}{ "CreateMetadataDbFilter": CreateMetadataDbFilter, "CreateDbFilter": CreateDbFilter, "AppendMessageHistory": AppendMessageHistory, + "ExtractFieldsFromQuery": ExtractFieldsFromQuery, + "PerformLLMRephraseRequest": PerformLLMRephraseRequest, } // PerformVectorEmbeddingRequest performs a vector embedding request to LLM @@ -982,3 +988,106 @@ func AppendMessageHistory(newMessage string, role AppendMessageHistoryRole, hist return history } + +// ExtractFieldsFromQuery extracts the fields from the user query +// +// Parameters: +// - query: the user query +// - fieldValues: the field values that the user query can contain +// - defaultFields: the default fields that the user query can contain +// +// Returns: +// - fields: the extracted fields +func ExtractFieldsFromQuery(query string, fieldValues map[string][]string, defaultFields []DefaultFields) (fields map[string]string) { + // Initialize the fields map + fields = make(map[string]string) + + // Check each field + for field, values := range fieldValues { + + // Initializing the field with None + fields[field] = "" + + // Sort the values by length in descending order + sort.Slice(values, func(i, j int) bool { + return len(values[i]) > len(values[j]) + }) + + // Check each possible value for exact match ignoring case + for _, value := range values { + if strings.Contains(strings.ToLower(query), strings.ToLower(value)) { + fields[field] = value + fmt.Println("Exact match found for", field, ":", value) + break + } + } + + // Split the query into words + words := strings.Fields(query) + + // If no exact match found, use fuzzy matching + if fields[field] == "" { + cutoff := 0.75 + cm := closestmatch.New(words, []int{3}) + for _, value := range values { + for _, word := range strings.Fields(value) { + closestWord := cm.Closest(word) + distance := levenshtein.RatioForStrings([]rune(word), []rune(closestWord), levenshtein.DefaultOptions) + if distance >= cutoff { + fields[field] = value + fmt.Println("Fuzzy match found for", field, ":", distance) + break + } + } + } + } + } + + // If default value is found, use it + for _, defaultField := range defaultFields { + value, ok := fields[defaultField.FieldName] + if ok && value == "" { + if strings.Contains(strings.ToLower(query), strings.ToLower(defaultField.QueryWord)) { + fields[defaultField.FieldName] = defaultField.FieldDefaultValue + fmt.Println("Default value found for", defaultField.FieldName, ":", defaultField.FieldDefaultValue) + } + } + } + + return fields +} + +// PerformLLMRephraseRequest performs a rephrase request to LLM +// +// Parameters: +// - template: the template for the rephrase request +// - query: the user query +// - history: the conversation history +// +// Returns: +// - rephrasedQuery: the rephrased query +func PerformLLMRephraseRequest(template string, query string, history []HistoricMessage) (rephrasedQuery string) { + // Append messages with conversation entries + historyMessages := "" + for _, entry := range history { + switch entry.Role { + case "user": + historyMessages += "HumanMessage(content): " + entry.Content + "\n" + case "assistant": + historyMessages += "AIMessage(content): " + entry.Content + "\n" + } + } + + // Create map for the data to be used in the template + dataMap := make(map[string]string) + dataMap["query"] = query + dataMap["chat_history"] = historyMessages + + // Format the template + systemTemplate := formatTemplate(template, dataMap) + + // Perform the general request + rephrasedQuery, _ = PerformGeneralRequest(query, history, false, systemTemplate) + + return rephrasedQuery +} diff --git a/pkg/externalfunctions/externalfunctions_test.go b/pkg/externalfunctions/externalfunctions_test.go new file mode 100644 index 0000000..b5bc30b --- /dev/null +++ b/pkg/externalfunctions/externalfunctions_test.go @@ -0,0 +1,88 @@ +package externalfunctions + +import ( + "testing" +) + +func TestExtractFieldsFromQuery(t *testing.T) { + fieldValues := map[string][]string{ + "physics": {"structures", "fluids", "electronics", "structural mechanics", "discovery", "optics", "photonics", "python", "scade", "materials", "stem", "student", "fluid dynamics", "semiconductors"}, + "type_of_asset": {"aic", "km", "documentation", "youtube", "general_faq", "alh", "article", "white-paper", "brochure"}, + "product": {"forte", "scade", "mechanical", "mechanical apdl", "fluent", "embedded software", "avxcelerate", "designxplorer", "designmodeler", "cloud direct", "maxwell", "stk", "ls-dyna", "lsdyna", "gateway", "granta", "rocky", "icepak", "siwave", "cfx", " meshing", " lumerical", "motion", "autodyn", "minerva", "redhawk-sc", "totem", "totem-sc", "powerartist", "raptorx", "velocerf", "exalto", "pathfinder", "pathfinder-sc", "diakopto", "pragonx", "primex", "on-chip electromagnetics", "redhawk-sc electrothermal", "redhawk-sc security", "voltage-timing and clock jitter", "medini", "ensight", "forte", "discovery", "hfss", "sherlock", "spaceclaim", "twin builder", "additive prep", "additive print", "composite cure sim", "composite preppost", "ncode designlife", "spaceclaim directmodeler", "cfx pre", "cfx solver", "cfx turbogrid", "icem cfd", "workbench platform"}, + } + + defaultFields := []DefaultFields{ + { + QueryWord: "course", + FieldName: "type_of_asset", + FieldDefaultValue: "aic", + }, + { + QueryWord: "apdl", + FieldName: "product", + FieldDefaultValue: "ls-dyna", + }, + { + QueryWord: "lsdyna", + FieldName: "product", + FieldDefaultValue: "ls-dyna", + }, + } + + testCases := []struct { + query string + want map[string]string + }{ + { + query: "The contact tool in which branch of the model tree informs the user whether the contact pair is initially open? Please choose from the following options: Geometry, Connections, Model, or Solution.", + want: map[string]string{}, + }, + { + query: "Which of the following controls/options are available under Analysis Setting in WB LS-DYNA documentation?", + want: map[string]string{"product": "ls-dyna", "type_of_asset": "documentation"}, + }, + { + query: "How does bonded contact differ from Shared Topology?", + want: map[string]string{}, + }, + { + query: "I'm interested in understanding how the residual vectors help in MSUP Harmonic Analysis. Can you list the KM on this topic?", + want: map[string]string{"type_of_asset": "km"}, + }, + { + query: "In the Mechanical Fatigue tool according to product documentaton, which option is used to specify the stress type for fatigue calculations ? Please choose the best option from the following: Equivalent Stress, Exposure Duration, Fatigue Strength Factor, or Stress Component.", + want: map[string]string{"product": "mechanical", "type_of_asset": "documentation"}, + }, + { + query: "Is there any courses available on Ansys Getting started with Mechanical?", + want: map[string]string{"product": "mechanical", "type_of_asset": "aic"}, + }, + { + query: "Can you check in Ansys help manual about the term 'remote pont'?", + want: map[string]string{"physics": "stem"}, + }, + { + query: "Is there any knowledge articles on how to define frictional contact in Ansys Mechanical?", + want: map[string]string{"product": "mechanical", "type_of_asset": "article"}, + }, + { + query: "Is there any knowledge materials on how to define frictional contact in Ansys Mechanical?", + want: map[string]string{"product": "mechanical", "type_of_asset": "materials"}, + }, + { + query: "Is there any KMs on how to model turbulent fluid flow in Ansys Fluent?", + want: map[string]string{"product": "fluent", "physics": "fluids", "type_of_asset": "km"}, + }, + } + + for _, tc := range testCases { + t.Run(tc.query, func(t *testing.T) { + result := ExtractFieldsFromQuery(tc.query, fieldValues, defaultFields) + for key, value := range tc.want { + if result[key] != value { + t.Errorf("For query %q, expected %s: %s, but got %s", tc.query, key, value, result[key]) + } + } + }) + } +} diff --git a/pkg/externalfunctions/privatefunctions.go b/pkg/externalfunctions/privatefunctions.go index f98fc48..af566f5 100644 --- a/pkg/externalfunctions/privatefunctions.go +++ b/pkg/externalfunctions/privatefunctions.go @@ -489,3 +489,18 @@ func validatePythonCode(pythonCode string) (bool, bool, error) { return false, false, fmt.Errorf("code validation failed: %s", output) } } + +// formatTemplate formats a template string with the given data +// +// Parameters: +// - template: the template string +// - data: the data to be used for formatting +// +// Returns: +// - string: the formatted template string +func formatTemplate(template string, data map[string]string) string { + for key, value := range data { + template = strings.ReplaceAll(template, `{`+key+`}`, value) + } + return template +} diff --git a/pkg/externalfunctions/types.go b/pkg/externalfunctions/types.go index 5c13418..4a3b4c6 100644 --- a/pkg/externalfunctions/types.go +++ b/pkg/externalfunctions/types.go @@ -256,3 +256,10 @@ type summaryCounters struct { ConstraintsAdded int `json:"constraints_added"` ConstraintsRemoved int `json:"constraints_removed"` } + +// DefaultFields represents the default fields for the user query. +type DefaultFields struct { + QueryWord string + FieldName string + FieldDefaultValue string +}