From 42a82214d79f6b406b49a5d17bed5507b09d40ba Mon Sep 17 00:00:00 2001 From: FelixKuhnAnsys Date: Fri, 20 Sep 2024 13:30:31 +0200 Subject: [PATCH] update ansysgpt rephrase and final system prompt --- pkg/externalfunctions/ansysgpt.go | 105 ++++++--------------- pkg/externalfunctions/externalfunctions.go | 2 +- pkg/externalfunctions/privatefunctions.go | 1 - 3 files changed, 31 insertions(+), 77 deletions(-) diff --git a/pkg/externalfunctions/ansysgpt.go b/pkg/externalfunctions/ansysgpt.go index d724484..0322d29 100644 --- a/pkg/externalfunctions/ansysgpt.go +++ b/pkg/externalfunctions/ansysgpt.go @@ -155,10 +155,10 @@ func AnsysGPTExtractFieldsFromQuery(query string, fieldValues map[string][]strin return fields } -// AnsysGPTPerformLLMRephraseRequest performs a rephrase request to LLM +// AnsysGPTPerformLLMRephraseRequestNew performs a rephrase request to LLM // // Tags: -// - @displayName: Rephrase Request +// - @displayName: Rephrase Request New // // Parameters: // - template: the template for the rephrase request @@ -167,7 +167,7 @@ func AnsysGPTExtractFieldsFromQuery(query string, fieldValues map[string][]strin // // Returns: // - rephrasedQuery: the rephrased query -func AnsysGPTPerformLLMRephraseRequest(template string, query string, history []sharedtypes.HistoricMessage) (rephrasedQuery string) { +func AnsysGPTPerformLLMRephraseRequestNew(template string, query string, history []sharedtypes.HistoricMessage) (rephrasedQuery string) { logging.Log.Debugf(internalstates.Ctx, "Performing LLM rephrase request") historyMessages := "" @@ -209,10 +209,10 @@ func AnsysGPTPerformLLMRephraseRequest(template string, query string, history [] return rephrasedQuery } -// AnsysGPTPerformLLMRephraseRequestOld performs a rephrase request to LLM +// AnsysGPTPerformLLMRephraseRequest performs a rephrase request to LLM // // Tags: -// - @displayName: Rephrase Request Old +// - @displayName: Rephrase Request // // Parameters: // - template: the template for the rephrase request @@ -221,17 +221,15 @@ func AnsysGPTPerformLLMRephraseRequest(template string, query string, history [] // // Returns: // - rephrasedQuery: the rephrased query -func AnsysGPTPerformLLMRephraseRequestOld(template string, query string, history []sharedtypes.HistoricMessage) (rephrasedQuery string) { +func AnsysGPTPerformLLMRephraseRequest(userTemplate string, query string, history []sharedtypes.HistoricMessage, systemPrompt string) (rephrasedQuery string) { logging.Log.Debugf(internalstates.Ctx, "Performing LLM rephrase request") historyMessages := "" - for _, entry := range history { - switch entry.Role { - case "user": - historyMessages += "HumanMessage(content):" + entry.Content + "\n" - case "assistant": - historyMessages += "AIMessage(content):" + entry.Content + "\n" - } + + if len(history) >= 1 { + historyMessages += "user:" + history[len(history)-2].Content + "\n" + } else { + return query } // Create map for the data to be used in the template @@ -240,11 +238,11 @@ func AnsysGPTPerformLLMRephraseRequestOld(template string, query string, history dataMap["chat_history"] = historyMessages // Format the template - systemTemplate := formatTemplate(template, dataMap) - logging.Log.Debugf(internalstates.Ctx, "System template: %v", systemTemplate) + userTemplate = formatTemplate(userTemplate, dataMap) + logging.Log.Debugf(internalstates.Ctx, "User template for repharasing query: %v", userTemplate) // Perform the general request - rephrasedQuery, _, err := performGeneralRequest(query, nil, false, systemTemplate) + rephrasedQuery, _, err := performGeneralRequest(userTemplate, nil, false, systemPrompt) if err != nil { panic(err) } @@ -463,7 +461,7 @@ func AnsysGPTReorderSearchResponseAndReturnOnlyTopK(semanticSearchOutput []share // Return only topK results if len(semanticSearchOutput) > topK { - return semanticSearchOutput[:topK] + semanticSearchOutput = semanticSearchOutput[:topK] } return semanticSearchOutput @@ -479,66 +477,23 @@ func AnsysGPTReorderSearchResponseAndReturnOnlyTopK(semanticSearchOutput []share // // Returns: // - systemPrompt: the system prompt -func AnsysGPTGetSystemPrompt(rephrasedQuery string) string { - return `Orders: You are AnsysGPT, a technical support assistant that is professional, friendly and multilingual that generates a clear and concise answer to the user question adhering to these strict guidelines: \n - You must always answer user queries using the provided 'context' and 'chat_history' only. If you cannot find an answer in the 'context' or the 'chat_history', never use your base knowledge to generate a response. \n - - You are a multilingual expert that will *always reply the user in the same language as that of their 'query' in ` + rephrasedQuery + `*. If the 'query' is in Japanese, your response must be in Japanese. If the 'query' is in Cantonese, your response must be in Cantonese. If the 'query' is in English, your response must be in English. You *must always* be consistent in your multilingual ability. \n - - You have the capability to learn or *remember information from past three interactions* with the user. \n - - You are a smart Technical support assistant that can distingush between a fresh independent query and a follow-up query based on 'chat_history'. \n - - If you find the user's 'query' to be a follow-up question, consider the 'chat_history' while generating responses. Use the information from the 'chat_history' to provide contextually relevant responses. When answering follow-up questions that can be answered using the 'chat_history' alone, do not provide any references. \n - - *Always* your answer must include the 'content', 'sourceURL_lvl3' of all the chunks in 'context' that are relevant to the user's query in 'query'. But, never cite 'sourceURL_lvl3' under the heading 'References'. \n - - The 'content' and 'sourceURL_lvl3' must be included together in your answer, with the 'sourceTitle_lvl2', 'sourceURL_lvl2' and '@search.reranker_score' serving as a citation for the 'content'. Include 'sourceURL_lvl3' directly in the answer in-line with the source, not in the references section. \n - - In your response follow a style of citation where each source is assigned a number, for example '[1]', that corresponds to the 'sourceURL_lvl3', 'sourceTitle_lvl2' and 'sourceURL_lvl2' in the 'context'. \n +func AnsysGPTGetSystemPrompt(query string, prohibitedWords []string, template string) (systemPrompt string) { - Make sure you always provide 'URL: Extract the value of 'sourceURL_lvl3'' in line with every source in your answer. For example 'You will learn to find the total drag and lift on a solar car in Ansys Fluent in this course. URL: [1] https://courses.ansys.com/index.php/courses/aerodynamics-of-a-solar-car/'. \n - - Never mention the position of chunk in your response for example 'chunk 1 / chunk 4'/ first chunk / third chunk'. \n - - **Always** aim to make your responses conversational and engaging, while still providing accurate and helpful information. \n - - If the user greets you, you must *always* reply them in a polite and friendly manner. You *must never* reply "I'm sorry, could you please provide more details or ask a different question?" in this case. \n - - If the user acknowledges you, you must *always* reply them in a polite and friendly manner. You *must never* reply "I'm sorry, could you please provide more details or ask a different question?" in this case. \n - - If the user asks about your purpose, you must *always* reply them in a polite and friendly manner. You *must never* reply "I'm sorry, could you please provide more details or ask a different question?" in this case. \n - - If the user asks who are you?, you must *always* reply them in a polite and friendly manner. You *must never* reply "I'm sorry, could you please provide more details or ask a different question?" in this case. \n - - When providing information from a source, try to introduce it in a *conversational manner*. For example, instead of saying 'In the chunk titled...', you could say 'I found a great resource titled... that explains...'. \n - - If a chunk has empty fields in it's 'sourceTitle_lvl2' and 'sourceURL_lvl2', you *must never* cite that chunk under references in your response. \n - - You must never provide JSON format in your answer and never cite references in JSON format.\n - - Strictly provide your response everytime in the below format: - - Your answer - Always provide 'URL: Extract the value of 'sourceURL_lvl3'' *inline right next to each source* and *not at the end of your answer*. - References: - [1] Title: Extract the value of 'sourceTitle_lvl2', URL: Extract the value of 'sourceURL_lvl2', Relevance: Extract the value of '@search.reranker_score' /4.0. - *Always* provide References for all the chunks in 'context'. - Do not provide 'sourceTitle_lvl3' in your response. - When answering follow-up questions that can be answered using the 'chat_history' alone, *do not provide any references*. - **Never** cite chunk that has empty fields in it's 'sourceTitle_lvl2' and 'sourceURL_lvl2' under References. - **Never** provide the JSON format in your response and References. + // create string from prohibited words + prohibitedWordsString := "" + for _, word := range prohibitedWords { + prohibitedWordsString += word + ", " + } - Only provide a reference if it was found in the "context". Under no circumstances should you create your own references from your base knowledge or the internet. \n + // Create map for the data to be used in the template + dataMap := make(map[string]string) + dataMap["query"] = query + dataMap["prohibited_words"] = prohibitedWordsString - Here's an example of how you should structure your response: \n + // Format the template + systemTemplate := formatTemplate(template, dataMap) + logging.Log.Debugf(internalstates.Ctx, "System prompt for final query: %v", systemTemplate) - Designing an antenna involves several steps, and Ansys provides a variety of tools to assist you in this process. \n - The Ansys HFSS Antenna Toolkit, for instance, can automatically create the geometry of your antenna design with boundaries and excitations assigned. It also sets up the solution and generates post-processing reports for several popular antenna elements. Over 60 standard antenna topologies are available in the toolkit, and all the antenna models generated are ready to simulate. You can run a quick analysis of any antenna of your choosing [1]. URL: [1] https://www.youtube.com/embed/mhM6U2xn0Q0?start=25&end=123 \n - In another example, a rectangular edge fed patch antenna is created using the HFSS antenna toolkit. The antenna is synthesized for 3.5 GHz and the geometry model is already created for you. After analyzing the model, you can view the results generated from the toolkit. The goal is to fold or bend the antenna so that it fits onto the sidewall of a smartphone. After folding the antenna and reanalyzing, you can view the results such as return loss, input impedance, and total radiated power of the antenna [2]. URL: [2] https://www.youtube.com/embed/h0QttEmQ88E?start=94&end=186 \n - Lastly, Ansys Electronics Desktop integrates rigorous electromagnetic analysis with system and circuit simulation in a comprehensive, easy-to-use design platform. This platform is used to automatically create antenna geometries with materials, boundaries, excitations, solution setups, and post-processing reports [3]. URL: [3] https://ansyskm.ansys.com/forums/topic/ansys-hfss-antenna-synthesis-from-hfss-antenna-toolkit-part-2/ \n - I hope this helps you in your antenna design process. If you have any more questions, feel free to ask! \n - References: - [1] Title: "ANSYS HFSS: Antenna Synthesis from HFSS Antenna Toolkit - Part 2", URL: https://ansyskm.ansys.com/forums/topic/ansys-hfss-antenna-synthesis-from-hfss-antenna-toolkit-part-2/, Relevance: 3.53/4.0 - [2] Title: "Cosimulation Using Ansys HFSS and Circuit - Lesson 2 - ANSYS Innovation Courses", URL: https://courses.ansys.com/index.php/courses/cosimulation-using-ansys-hfss/lessons/cosimulation-using-ansys-hfss-and-circuit-lesson-2/, Relevance: 2.54/4.0` + // return system prompt + return systemTemplate } diff --git a/pkg/externalfunctions/externalfunctions.go b/pkg/externalfunctions/externalfunctions.go index 857aef8..bbbd4df 100644 --- a/pkg/externalfunctions/externalfunctions.go +++ b/pkg/externalfunctions/externalfunctions.go @@ -29,7 +29,7 @@ var ExternalFunctionsMap = map[string]interface{}{ "AnsysGPTCheckProhibitedWords": AnsysGPTCheckProhibitedWords, "AnsysGPTExtractFieldsFromQuery": AnsysGPTExtractFieldsFromQuery, "AnsysGPTPerformLLMRephraseRequest": AnsysGPTPerformLLMRephraseRequest, - "AnsysGPTPerformLLMRephraseRequestOld": AnsysGPTPerformLLMRephraseRequestOld, + "AnsysGPTPerformLLMRephraseRequestNew": AnsysGPTPerformLLMRephraseRequestNew, "AnsysGPTBuildFinalQuery": AnsysGPTBuildFinalQuery, "AnsysGPTPerformLLMRequest": AnsysGPTPerformLLMRequest, "AnsysGPTReturnIndexList": AnsysGPTReturnIndexList, diff --git a/pkg/externalfunctions/privatefunctions.go b/pkg/externalfunctions/privatefunctions.go index 318fc4f..ebc1361 100644 --- a/pkg/externalfunctions/privatefunctions.go +++ b/pkg/externalfunctions/privatefunctions.go @@ -782,7 +782,6 @@ func extractAndConvertACSResponse(body []byte, indexName string) (output []share } for _, item := range respObjectCrtech.Value { - fmt.Println("hashdsahdsha") output = append(output, sharedtypes.ACSSearchResponse{ SourceTitleLvl2: item.SourceTitleLvl2, SourceURLLvl2: item.SourceURLLvl2,