Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ansys gpt functions (#26) #27

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
109 changes: 109 additions & 0 deletions pkg/externalfunctions/externalfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand All @@ -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
Expand Down Expand Up @@ -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
}
88 changes: 88 additions & 0 deletions pkg/externalfunctions/externalfunctions_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
})
}
}
15 changes: 15 additions & 0 deletions pkg/externalfunctions/privatefunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions pkg/externalfunctions/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading