From 8f54bb896dba808d194325aedd442fab9d64dab8 Mon Sep 17 00:00:00 2001
From: Christian Boulanger <boulanger@lhlt.mpg.de>
Date: Tue, 16 Jan 2024 19:52:37 +0100
Subject: [PATCH] add huggingface inference endpoint

---
 langchain-experiments/compare-models.ipynb    | 13 +++++++------
 .../data/output/editors-openai-gpt-3.5-turbo  | 18 ++++++++++++++++++
 .../data/output/editors-openai-gpt4           | 19 +++++++++++++++++++
 .../lib/hf_llama2_13b_chat.py                 |  8 +++++---
 .../{data/output => tmp}/.gitignore           |  0
 5 files changed, 49 insertions(+), 9 deletions(-)
 create mode 100644 langchain-experiments/data/output/editors-openai-gpt-3.5-turbo
 create mode 100644 langchain-experiments/data/output/editors-openai-gpt4
 rename langchain-experiments/{data/output => tmp}/.gitignore (100%)

diff --git a/langchain-experiments/compare-models.ipynb b/langchain-experiments/compare-models.ipynb
index 73aa8b1..2cea24f 100644
--- a/langchain-experiments/compare-models.ipynb
+++ b/langchain-experiments/compare-models.ipynb
@@ -7,8 +7,8 @@
    "metadata": {
     "collapsed": true,
     "ExecuteTime": {
-     "end_time": "2024-01-16T18:33:26.160935700Z",
-     "start_time": "2024-01-16T18:33:24.612516500Z"
+     "end_time": "2024-01-16T18:51:39.319347300Z",
+     "start_time": "2024-01-16T18:51:32.944085Z"
     }
    },
    "outputs": [],
@@ -144,7 +144,7 @@
    "outputs": [
     {
      "data": {
-      "text/plain": "[{'generated_text': '  Here is the data in CSV format as requested:\\n\\n\\n\"lastname\", \"firstname\", \"title\", \"position\", \"affiliation\", \"role\"\\n\"\\n\"Martinez, Jose\", \"Professor\", \"Georg-August-Universitat Göttingen\", \"Editor\"\\nBusse, Christian\", \"Regierungsdirector\", \"Agrarprodukt\"\\nEndres, Ewald\", \"Professor\", \"Hochst'}]"
+      "text/plain": "['',\n 'Martinez,José,Professor',\n 'Busse,Regierungsdirektor',\n 'Ewald,Endres,Forst,Jurist',\n 'Glas,VolkmarNies,Koch',\n '',\n '',\n 'Note: Some positions have been omitted as they are not explicitly mentioned in the text.']"
      },
      "execution_count": 2,
      "metadata": {},
@@ -154,13 +154,14 @@
    "source": [
     "from lib.hf_llama2_13b_chat import query\n",
     "\n",
-    "query(template, journal_name=journal_name, website_text=website_text)"
+    "lines = query(template, journal_name=journal_name, website_text=website_text).split(\"\\n\")[4:]\n",
+    "lines"
    ],
    "metadata": {
     "collapsed": false,
     "ExecuteTime": {
-     "end_time": "2024-01-16T18:33:36.644916700Z",
-     "start_time": "2024-01-16T18:33:33.232793300Z"
+     "end_time": "2024-01-16T18:51:42.730907700Z",
+     "start_time": "2024-01-16T18:51:39.319347300Z"
     }
    },
    "id": "f05098a4cf2aa3dc"
diff --git a/langchain-experiments/data/output/editors-openai-gpt-3.5-turbo b/langchain-experiments/data/output/editors-openai-gpt-3.5-turbo
new file mode 100644
index 0000000..b96f5a4
--- /dev/null
+++ b/langchain-experiments/data/output/editors-openai-gpt-3.5-turbo
@@ -0,0 +1,18 @@
+,lastname,firstname,title,position,affiliation,role
+0,Deutsche Gesellschaft für Agrarrecht, , ,Herausgeber, , 
+1,Regierungsdirektor,Dr. Christian, ,Redaktion,"Bundesministerium für Ernährung und Landwirtschaft, Bonn", 
+2,Prof. Dr.,Ewald, ,Redaktion,"Hochschule Weihenstephan-Triesdorf, Freising", 
+3,Rechtsanwalt,Dr. Matthias, ,Redaktion,Bitburg, 
+4,Rechtsanwalt,Dr. Bernd,von Garmissen,Redaktion,Göttingen, 
+5,Rechtsanwalt,Ingo, ,Redaktion,Rostock, 
+6,Rechtsanwältin,Christiane, ,Redaktion,Bonn, 
+7,Rechtsanwalt,Jens, ,Redaktion,Peine, 
+8,Ltd. Verwaltungsdirektor,Dr. Erich, ,Redaktion,"Sozialversicherung für Landwirtschaft, Forsten und Gartenbau, Kassel", 
+9,Ministerialrat,Dr. Christian, ,Redaktion,"Bayerisches Staatsministerium für Ernährung, Landwirtschaft und Forsten, München", 
+10,Prof. Dr.,Jose, ,Redaktion,"Institut für Landwirtschaftsrecht, Georg-August-Universitat Göttingen, Göttingen", 
+11,Ltd. Landwirtschaftsdirektor,Volkmar, ,Redaktion,"Landwirtschaftskammer NRW, Bonn", 
+12,Rechtsanwalt/Steuerberater,Ralf, ,Redaktion,Bonn, 
+13,Rechtsanwalt,Harald, ,Redaktion,"Landvolk Niedersachsen, Hannover", 
+14,Prof. Dr.,José, ,Schriftleitung,Institut für Landwirtschaftsrecht, 
+15,LLD,Volkmar, ,Schriftleitung, , 
+16,Irina Valeska,Schell, ,Assistenz der Schriftleitung, , 
diff --git a/langchain-experiments/data/output/editors-openai-gpt4 b/langchain-experiments/data/output/editors-openai-gpt4
new file mode 100644
index 0000000..649cf34
--- /dev/null
+++ b/langchain-experiments/data/output/editors-openai-gpt4
@@ -0,0 +1,19 @@
+,lastname,firstname,title,position,affiliation,role
+0,Deutsche Gesellschaft für Agrarrecht,,,,,Herausgeber
+1,Busse,Christian,Dr.,Regierungsdirektor,"Bundesministerium für Ernährung und Landwirtschaft, Bonn",Redaktion
+2,Endres,Ewald,Prof. Dr.,,"Hochschule Weihenstephan-Triesdorf, Freising",Redaktion
+3,Francois,Matthias,Dr.,Rechtsanwalt,,Redaktion
+4,von Garmissen,Bernd,Dr.,Rechtsanwalt,,Redaktion
+5,Glas,Ingo,Dr.,Rechtsanwalt,,Redaktion
+6,Graß,Christiane,,Rechtsanwältin,,Redaktion
+7,Haarstrich,Jens,,Rechtsanwalt,,Redaktion
+8,Koch,Erich,Dr.,Ltd. Verwaltungsdirektor,"Sozialversicherung für Landwirtschaft, Forsten und Gartenbau, Kassel",Redaktion
+9,Köpl,Christian,Dr.,Ministerialrat,"Bayerisches Staatsministerium für Ernährung, Landwirtschaft und Forsten, München",Redaktion
+10,Martinez,Jose,Prof. Dr.,,"Institut für Landwirtschaftsrecht, Georg-August-Universitat Göttingen, Göttingen",Redaktion
+11,Nies,Volkmar,,Ltd. Landwirtschaftsdirektor,"Landwirtschaftskammer NRW, Bonn",Redaktion
+12,Stephany,Ralf,,Rechtsanwalt/Steuerberater,,Redaktion
+13,Wedemeyer,Harald,,Rechtsanwalt,"Landvolk Niedersachsen, Hannover",Redaktion
+14,Institut für Landwirtschaftsrecht,,,,,Schriftleitung
+15,Martinez,José,Prof. Dr.,Erster Schriftleiter,Institut für Landwirtschaftsrecht,Schriftleitung
+16,Nies,Volkmar,LLD,Zweiter Schriftleiter,,Schriftleitung
+17,Schell,Irina Valeska,,Assistenz der Schriftleitung,,Schriftleitung
diff --git a/langchain-experiments/lib/hf_llama2_13b_chat.py b/langchain-experiments/lib/hf_llama2_13b_chat.py
index a454be0..a2418cb 100644
--- a/langchain-experiments/lib/hf_llama2_13b_chat.py
+++ b/langchain-experiments/lib/hf_llama2_13b_chat.py
@@ -21,12 +21,14 @@ def query(template, model_params = None, **params):
         }
     prompt = template.format_map(params)
     payload = {
-        "inputs": f"<s>[INST] <<SYS>>You are a helpful assistant. You keep your answers short. When asked to provide data as response, do not return additional comments.<</SYS>>{prompt}[/INST]",
+        "inputs": f"<s>[INST] <<SYS>>You are a helpful assistant. You keep your answers short. When asked to provide data such as CSV data or JSON as response, do not return additional comments or notes.<</SYS>>{prompt}[/INST]",
         "parameters": model_params
     }
     response = requests.post(API_URL, headers=headers, json=payload)
     response.raise_for_status()
     try:
-        return response.json()
+        return response.json()[0].get("generated_text")
     except JSONDecodeError:
-        raise RuntimeError(f'Cannot parse response from {response.url}.')
+        with open('tmp/response.txt', "w", encoding='utf-8') as f:
+            f.write(response.text)
+        raise RuntimeError(f'Cannot parse response from {response.url}. See tmp/response.txt')
diff --git a/langchain-experiments/data/output/.gitignore b/langchain-experiments/tmp/.gitignore
similarity index 100%
rename from langchain-experiments/data/output/.gitignore
rename to langchain-experiments/tmp/.gitignore
-- 
GitLab