1414public class RagDemo {
1515
1616 static {
17- Unirest .config ().socketTimeout (120000 ). connectTimeout ( 120000 );
17+ Unirest .config ().socketTimeout (5 * 60 * 1000 );
1818 }
1919
2020 public static void main (String [] args ) throws Exception {
2121 var input = System .console ().readLine ("I'm looking for: " );
2222
23- System .out .println ("\n Finding closest products..." );
23+ System .out .println ("Finding closest products..." );
2424 var context = getContext (input );
25- System .out .println ("-------------------------------------------------------------" );
26- System .out .println (context );
27- System .out .println ("-------------------------------------------------------------\n " );
28- 2925 System .out .println ("Generating response..." );
3026 var prompt = buildPrompt (input , context );
3127 var response = getResponse (prompt );
@@ -34,10 +30,13 @@ public static void main(String[] args) throws Exception {
3430
3531 private static String getContext (String input ) throws Exception {
3632 var requestBody = """
37- { "model": "bert-cpp-minilm-v6", "input": %s }
33+ {
34+ "model": "bert-cpp-minilm-v6",
35+ "input": %s
36+ }
3837 """ .formatted (new ObjectMapper ().writeValueAsString (input ));
3938
40- var response = Unirest .post ("http://localhost :8080/v1/embeddings" )
39+ var response = Unirest .post ("http://127.0.0.1 :8080/v1/embeddings" )
4140 .header ("Content-Type" , "application/json" )
4241 .body (requestBody )
4342 .asString ().getBody ();
@@ -53,7 +52,7 @@ SELECT id, CONCAT(
5352 FROM products
5453 WHERE embedding IS NOT NULL
5554 ORDER BY VEC_Distance(embedding, VEC_FromText(JSON_EXTRACT(:response, '$.data[0].embedding')))
56- LIMIT 10
55+ LIMIT 7
5756 """ )
5857 .addParameter ("response" , response )
5958 .executeAndFetchTable ();
@@ -66,20 +65,26 @@ ORDER BY VEC_Distance(embedding, VEC_FromText(JSON_EXTRACT(:response, '$.data[0]
6665
6766 private static String buildPrompt (String input , Object context ) {
6867 return """
69- You are a sales assistant. I'm looking for %s.
70-
71- Using the following information, recommend me a product in one single paragraph:
68+ I'm looking for %s. Using the following information, recommend me a product in one single paragraph:
7269
7370 %s
7471 """ .formatted (input , context );
7572 }
7673
7774 private static String getResponse (String prompt ) throws Exception {
7875 var requestBody = """
79- { "model": "phi-2", "messages": [{"role": "user", "content": %s, "temperature": 0.4}] }
76+ {
77+ "model": "phi-2-chat",
78+ "messages": [
79+ {"role": "system", "content": "You are a sales assistant."},
80+ {"role": "user", "content": %s}
81+ ],
82+ "temperature": 0.2,
83+ "max_tokens": 100
84+ }
8085 """ .formatted (new ObjectMapper ().writeValueAsString (prompt ));
8186
82- var response = Unirest .post ("http://localhost :8080/v1/chat/completions" )
87+ var response = Unirest .post ("http://127.0.0.1 :8080/v1/chat/completions" )
8388 .header ("Content-Type" , "application/json" )
8489 .body (requestBody )
8590 .asString ().getBody ();
0 commit comments