Skip to content
Snippets Groups Projects
Commit 167d0d5c authored by Christian Boulanger's avatar Christian Boulanger
Browse files

commit changes to langchain-experiments

parent 434d66d1
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:396ebd275b60c720 tags:
# Comparing OpenAI and open LLMs
Using the [text-only content of the website of the journal AUR - Agrar- und Umweltrecht](data/input/journal-website.txt),
we compare the performance of GPT-4, GPT-3.5-turbo and Models available on Huggingface.
## Preparation
Import dependencies, define shorthand functions, and prepare test data
%% Cell type:code id:e46d0648c1c6c96a tags:
``` python
import io
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
import pandas as pd
from dotenv import load_dotenv
load_dotenv()
def response_to_df(response):
data = io.StringIO(response)
try:
return pd.read_csv(data)
except:
raise RuntimeError(f"Error while parsing response:\n{response}")
def use_model(model, template, **params):
prompt = ChatPromptTemplate.from_template(template)
chain = (
prompt
| model
| StrOutputParser()
)
return response_to_df(chain.invoke(params))
with open('data/input/journal-website.txt', encoding='utf-8') as f:
website_text = f.read()
journal_name = "AUR - Agrar- und Umweltrecht"
```
%% Cell type:markdown id:eb805270480fbfb5 tags:
## Prompt
OpenAI's GPT-4 works perfectly with a minimal, German-language prompt, and infers the meaning of the columns
to returns the data we need:
```
Finde im folgenden Text die Herausgeber, Redaktion/Schriftleitung und Beirat der Zeitschrift '{journal_name}' und gebe sie im CSV-Format zurück mit den Spalten 'lastname', 'firstname', 'title', 'position', 'affiliation','role'. Die Spalte 'role' enthält entweder 'Herausgeber', 'Redaktion', 'Beirat', 'Schriftleitung' oder ist leer wenn nicht bestimmbar. Wenn keine passenden Informationen verfügbar sind, gebe nur den CSV-Header zurück. Setze alle Werte in den CSV-Spalten in Anführungszeichen."
````
In contrast, the open models performed miserably with such a prompt. We therefore use English and provide very detailed instructions.
%% Cell type:code id:23aef80911796078 tags:
``` python
with open('data/input/journal-website.txt', encoding='utf-8') as f:
website_text = f.read()
journal_name = "AUR - Agrar- und Umweltrecht"
template = """
In the following German text, which was scraped from a website, find the members of the editorial board or the advisory board of the journal '{journal_name}' as per the following rules:
- In German, typical labels for these roles are "Herausgeber", "Redaktion/Redakteur/Schriftleitung" and "Beirat".
- Return the data as comma-separated values, which can be saved to a `.csv` file. Put all values in the CSV rows in quotes.
- The CSV data must have the columns 'lastname', 'firstname', 'title', 'position', 'affiliation','role'.
- The column 'role' must contain either 'Herausgeber', 'Redaktion', 'Beirat' or is empty. Leave the column empty if you cannot determine the role. Use 'Redaktion' for the "Schriftleitung" role.
- The column 'title' should contain academic titles such as "Dr." or "Prof. Dr."
- The column 'position' should contain the job title
- The column 'title' should contain academic titles, such as "Dr." or "Prof. Dr."
- The column 'position' should contain the job title, typically "Rechtsanwalt", "Regierungsrat" or "Richter am Oberlandesgericht"
- The column 'affiliation' contains the institution or organization the person belongs to, or the city if one is mentioned
- If the journal is published ("herausgeben von") by an association, institute or other organization, but its name in the column 'lastname'.
- If you cannot find any information, simply return the CSV header.
- You must not output any introduction, commentary or explanation such as 'Here is the CSV data for the members of the editorial board or the advisory board of the journal'. Only return the data.
{website_text}
"""
```
%% Cell type:markdown id:8f994c771cc9b4ef tags:
## ChatGPT-4
GPT-4 delivers an almost perfect [result](data/output/editors-openai-gpt-4.csv). There are some problems left which could be resolved by adding some more instructions to the prompt.
%% Cell type:code id:initial_id tags:
``` python
model = ChatOpenAI(model_name="gpt-4")
df = use_model(model, template, journal_name=journal_name, website_text=website_text)
df.to_csv('data/output/editors-openai-gpt-4.csv')
df
```
%% Output
lastname firstname title \
0 DGAR NaN NaN
1 Busse Christian Dr.
2 Endres Ewald Prof. Dr.
3 Francois Matthias Dr.
4 von Garmissen Bernd Dr.
5 Glas Ingo NaN
6 Graß Christiane NaN
7 Haarstrich Jens NaN
8 Koch Erich Dr.
9 Köpl Christian Dr.
10 Martinez Jose Prof. Dr.
11 Nies Volkmar NaN
12 Stephany Ralf NaN
13 Wedemeyer Harald NaN
14 Martinez José Prof. Dr.
15 Nies Volkmar LLD
position \
0 Deutsche Gesellschaft für Agrarrecht
1 Regierungsdirektor
2 NaN
3 Rechtsanwalt
4 Rechtsanwalt
5 Rechtsanwalt
6 Rechtsanwältin
7 Rechtsanwalt
8 Ltd. Verwaltungsdirektor
9 Ministerialrat
10 NaN
11 Ltd. Landwirtschaftsdirektor
12 Rechtsanwalt/Steuerberater
13 Rechtsanwalt
14 Erster Schriftleiter
15 Zweiter Schriftleiter
affiliation role
0 NaN Herausgeber
1 Bundesministerium für Ernährung und Landwirtsc... Redaktion
2 Hochschule Weihenstephan-Triesdorf, Freising Redaktion
3 Bitburg Redaktion
4 Göttingen Redaktion
5 Rostock Redaktion
6 Bonn Redaktion
7 Peine Redaktion
8 Sozialversicherung für Landwirtschaft, Forsten... Redaktion
9 Bayerisches Staatsministerium für Ernährung, L... Redaktion
10 Institut für Landwirtschaftsrecht, Georg-Augus... Redaktion
11 Landwirtschaftskammer NRW, Bonn Redaktion
12 Bonn Redaktion
13 Landvolk Niedersachsen, Hannover Redaktion
14 Institut für Landwirtschaftsrecht, Göttingen Redaktion
15 50170 Kerpen Redaktion
%% Cell type:markdown id:9cef77bb17d57b53 tags:
## ChatGPT 3.5-turbo
GPT-3.5 [performs less well](data/output/editors-openai-gpt-3.5-turbo.csv), but still ok. It gets some of the 'title' amd 'position'
column data confused, and does not recognize the institutional publisher (Herausgeber) of the journal.
%% Cell type:code id:e1aedc5ef3cab564 tags:
``` python
model = ChatOpenAI(model_name="gpt-3.5-turbo")
df = use_model(model, template, journal_name=journal_name, website_text=website_text)
df.to_csv('data/output/editors-openai-gpt-3.5-turbo.csv')
df
```
%% Output
lastname firstname title \
0 Busse Christian Dr.
1 Endres Ewald Prof. Dr.
2 Francois Matthias Dr.
3 von Garmissen Bernd Dr.
4 Glas Ingo NaN
5 Graß Christiane Rechtsanwältin
6 Haarstrich Jens Rechtsanwalt
7 Koch Erich Dr.
8 Köpl Christian Dr.
9 Martinez Jose Prof. Dr.
10 Nies Volkmar Ltd. Landwirtschaftsdirektor
11 Stephany Ralf Rechtsanwalt/Steuerberater
12 Wedemeyer Harald Rechtsanwalt
position \
0 Regierungsdirektor
1 NaN
2 Rechtsanwalt
3 Rechtsanwalt
4 Rechtsanwalt
5 NaN
6 NaN
7 Ltd. Verwaltungsdirektor
8 Ministerialrat
9 NaN
10 NaN
11 NaN
12 NaN
affiliation role
0 Bundesministerium für Ernährung und Landwirtsc... Redaktion
1 Hochschule Weihenstephan-Triesdorf, Freising Redaktion
2 Bitburg Redaktion
3 Göttingen Redaktion
4 Rostock Redaktion
5 Bonn Redaktion
6 Peine Redaktion
7 Sozialversicherung für Landwirtschaft, Forsten... Redaktion
8 Bayerisches Staatsministerium für Ernährung, L... Redaktion
9 Institut für Landwirtschaftsrecht, Georg-Augus... Redaktion
10 Landwirtschaftskammer NRW, Bonn Redaktion
11 Bonn Redaktion
12 Landvolk Niedersachsen, Hannover Redaktion
%% Cell type:markdown id:31084716e138fe06 tags:
Now, let's try the open models via the Huggingface Inference Endpoint. For this to work, you need to deploy
endpoints via https://ui.endpoints.huggingface.co/ and update the value of `enpoint_url` below.
%% Cell type:markdown id:90ce37abf037e19f tags:
## TheBloke/Llama-2-13B-chat-GPTQ
The [LLama2 13 billion parameter model](https://huggingface.co/TheBloke/Llama-2-13B-chat-GPTQ) produces [unusuable output](data/output/editors-llama-2-13b-chat-gptq.txt).
%% Cell type:code id:f05098a4cf2aa3dc tags:
``` python
from lib.hf_llama2_chat_gptq import query
llama2_template = f"<s>[INST] <<SYS>>You are a helpful assistant. No comments or explanation, just answer the question.<</SYS>>{template}[/INST]"
endpoint_url = "https://z8afrqamxvaaitmf.us-east-1.aws.endpoints.huggingface.cloud"
query(endpoint_url, template, journal_name=journal_name, website_text=website_text).split("\n")
```
%% Output
['Martinez, Dr. Christian Busse, Bundesministerium für Ernährung und Landwirtschaft, Bonn Agrarprodukt Recht',
'Prof. Dr. Ewald Endres, Hochschule Weihenstephan-Triesdorf Freising Forsting Forsting, Jagd, Fischerei, Fischerei',
'Lawyeranwalt Ingo Glas, Bitburg Boden Recht',
'Christiane Grass, Bonn Agrarzivil Recht',
'Jens Haarstrich, Peine Redaktionär, Rostock',
'Prof. Dr. Bernd von Garmissen, Göttingen Erb, Redaktion, Umwelt',
'Ltdr. Jose Martinez, Georg-August-Universität Göttingen, Göttingen',
'',
'',
"Note: The column 'Role' contains the following values: 'Herausgeber', 'Redaktion', 'Beirat'"]
%% Cell type:markdown id:ca33fb28f6772cbc tags:
## TheBloke/Llama-2-70B-chat-GPTQ via Huggingface Inference Endpoint
The 70 billion parameter variant [does a bit better](data/output/editors-llama-2-70b-chat-gptq.csv) but, among other things, doesn't the academic titles right. It also cannot be persuaded to [not comment on the CSV output](data/output/editors-llama-2-70b-chat-gptq.txt). Given that the model costs $13/h to run, the result is not impressive.
The 70 billion parameter variant [does a bit better](data/output/editors-llama-2-70b-chat-gptq.csv) but, among other things, doesn't the academic titles right. It also cannot be persuaded to [not comment on the CSV output](data/output/editors-llama-2-70b-chat-gptq.txt).
%% Cell type:code id:b94cf62b996bf3a2 tags:
``` python
endpoint_url = "https://gp8iviqlqee101a0.us-east-1.aws.endpoints.huggingface.cloud"
query(endpoint_url, template, journal_name=journal_name, website_text=website_text).split("\n")
```
%% Cell type:markdown id:5b9b6d23dcdbbdd6 tags:
## mixtral-8x7b-instruct-v0-1-puk
%% Cell type:code id:a5eb505982b3aafe tags:
``` python
from lib.hf_llama2_chat_gptq import query
llama2_template = f"<s>[INST] <<SYS>>You are a helpful assistant. You answer the question without any further No comments or explanation.<</SYS>>{template}[/INST]"
endpoint_url = "https://pmxm9cba6f8uvi9s.us-east-1.aws.endpoints.huggingface.cloud"
query(endpoint_url, template, journal_name=journal_name, website_text=website_text).split("\n")
```
%% Cell type:code id:9d5b65beac1f863e tags:
``` python
from langchain_community.llms import HuggingFaceEndpoint
ENDPOINT_URL = "https://pmxm9cba6f8uvi9s.us-east-1.aws.endpoints.huggingface.cloud"
llm = HuggingFaceEndpoint(
endpoint_url=ENDPOINT_URL,
task="text-generation",
model_kwargs={
"max_new_tokens": 512,
"top_k": 50,
"temperature": 0.1,
"repetition_penalty": 1.03,
},
)
```
%% Cell type:code id:58e87b84bdc2e4d3 tags:
``` python
import os
from langchain_community.llms import HuggingFaceTextGenInference
ENDPOINT_URL = "https://pmxm9cba6f8uvi9s.us-east-1.aws.endpoints.huggingface.cloud"
HF_TOKEN = "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"
llm = HuggingFaceTextGenInference(
inference_server_url=ENDPOINT_URL,
max_new_tokens=512,
top_k=50,
temperature=0.1,
repetition_penalty=1.03,
server_kwargs={
"headers": {
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "application/json",
}
},
)
```
%% Cell type:code id:411c80521b6f7ccb tags:
``` python
from langchain.schema import (
HumanMessage,
SystemMessage
)
from langchain_community.chat_models.huggingface import ChatHuggingFace
messages = [
#SystemMessage(content="You're a helpful assistant"),
HumanMessage(
content="What happens when an unstoppable force meets an immovable object?"
),
]
chat_model = ChatHuggingFace(llm=llm)
```
%% Cell type:markdown id:9f2e4ce2c28741e0 tags:
%% Cell type:code id:2b2cbf175a71930b tags:
``` python
res = chat_model.invoke(messages)
print(res.content)
```
%% Output
' Here is the CSV data for the members of the editorial board or the advisory board of the journal \'AUR - Agrar- und Umweltrecht\':\n\n"lastname","firstname","title","affiliation","role"\n"Busse","Christian", "Regierungsdirektor", "Bundesministerium für Ernährung und Landwirtschaft, Bonn", "Herausgeber"\n"Endres","Ewald", "Prof. Dr.", "Hochschule Weihenstephan-Triesdorf, Freising", "Redaktion"\n"Francois","Matthias", "Rechtsanwalt", "Bitburg", "Redaktion"\n"Garmissen","Bernd", "Rechtsanwalt", "Göttingen", "Redaktion"\n"Graß","Christiane", "Rechtsanwältin", "Bonn", "Redaktion"\n"Haarstrich","Jens", "Rechtsanwalt", "Peine", "Redaktion"\n"Köpl","Christian", "Ministerialrat", "Bayerisches Staatsministerium für Ernährung, Landwirtschaft und Forsten, München", ""\n"Martinez","Jose", "Prof. Dr.", "Institut für Landwirtschaftsrecht, Georg-August-Universität Göttingen, Göttingen", "Herausgeber"\n"Nies","Volkmar", "Ltd. Landwirtschaftsdirektor", "Landwirtschaftskammer NRW, Bonn", "Redaktion"\n"Stephany","Ralf", "Rechtsanwalt/Steuerberater", "Bonn", "Redaktion"\n"Wedemeyer","Harald", "Rechtsanwalt", "Landvolk Niedersachsen, Hannover", "Redaktion"\n"Schell","Irina Valeska", "", "Georg-August-Universität Göttingen, Göttingen", ""\n\nNote: The column \'role\' is empty for some members, as their role could not be determined.'
This is a classic philosophical question that has been asked for centuries, often used to illustrate a paradox. The idea of an "unstoppable force" implies something that cannot be stopped or slowed down, while an "immovable object" suggests something that cannot be moved.
If we take these definitions literally, then when an unstoppable force meets an immovable object, it would result in a situation where neither can fulfill their inherent nature. This creates a paradox because the force cannot stop, but the object won't be moved.
In reality, such a scenario is impossible as it defies the laws of physics. Forces and objects in the universe do not possess these absolute qualities. Instead, forces can typically be slowed, redirected, or absorbed, and objects can usually be moved, albeit sometimes with great difficulty.
......
Georg-August-Universität Göttingen Institut für Landwirtschaftsrecht
Institut
Team
Forschungsprojekte
Lehre
Veranstaltungen
Göttinger Onlinebeiträge zum Agrarrecht
LexVinum
AUR
beck-blog
Startseite AUR
Institut
Team
Forschungsprojekte
Lehre
Veranstaltungen
Göttinger Onlinebeiträge zum Agrarrecht
LexVinum
AUR
beck-blog
Startseite AUR
Suchen English
AUR - Agrar- und Umweltrecht
......@@ -20,31 +20,31 @@ Die Zeitschrift Agrar- und Umweltrecht ist die führende Zeitschrift in Deutschl
Das Institut für Landwirtschaftsrecht ist der Sitz der Schriftleitung
Die Redaktion der Zeitschrift "Agrar-und Umweltrecht":
Regierungsdirektor Dr. Christian Busse, Bundesministerium für Ernährung und Landwirtschaft, Bonn (Agrarproduktrecht)
Regierungsdirektor Dr. Christian Busse, Bundesministerium für Ernährung und Landwirtschaft, Bonn (Agrarproduktrecht)
Prof. Dr. Ewald Endres, Hochschule Weihenstephan-Triesdorf, Freising (Forst, Jagd, Fischerei)
Prof. Dr. Ewald Endres, Hochschule Weihenstephan-Triesdorf, Freising (Forst, Jagd, Fischerei)
Rechtsanwalt Dr. Matthias Francois, Bitburg (Bodenrecht)
Rechtsanwalt Dr. Matthias Francois, Bitburg (Bodenrecht)
Rechtsanwalt Dr. Bernd von Garmissen, Göttingen (Erb- und Gesellschaftsrecht)
Rechtsanwalt Dr. Bernd von Garmissen, Göttingen (Erb- und Gesellschaftsrecht)
Rechtsanwalt Ingo Glas, Rostock (Ziviles Agrarwirtschaftsrecht)
Rechtsanwalt Ingo Glas, Rostock (Ziviles Agrarwirtschaftsrecht)
Rechtsanwältin Christiane Graß, Bonn (Agrarzivilrecht)
Rechtsanwältin Christiane Graß, Bonn (Agrarzivilrecht)
Rechtsanwalt Jens Haarstrich, Peine (Agrarzivilrecht)
Rechtsanwalt Jens Haarstrich, Peine (Agrarzivilrecht)
Ltd. Verwaltungsdirektor Dr. Erich Koch, Sozialversicherung für Landwirtschaft, Forsten und Gartenbau, Kassel (Agrarsozialrecht)
Ltd. Verwaltungsdirektor Dr. Erich Koch, Sozialversicherung für Landwirtschaft, Forsten und Gartenbau, Kassel (Agrarsozialrecht)
Ministerialrat Dr. Christian Köpl, Bayerisches Staatsministerium für Ernährung, Landwirtschaft und Forsten, München (Landwirtschaftliche Betriebsmittel)
Ministerialrat Dr. Christian Köpl, Bayerisches Staatsministerium für Ernährung, Landwirtschaft und Forsten, München (Landwirtschaftliche Betriebsmittel)
Prof. Dr. Jose Martinez, Institut für Landwirtschaftsrecht, Georg-August-Universitat Göttingen, Göttingen (Agrarförderrecht)
Prof. Dr. Jose Martinez, Institut für Landwirtschaftsrecht, Georg-August-Universitat Göttingen, Göttingen (Agrarförderrecht)
Ltd. Landwirtschaftsdirektor Volkmar Nies, Landwirtschaftskammer NRW, Bonn (Agrarumweltrecht)
Ltd. Landwirtschaftsdirektor Volkmar Nies, Landwirtschaftskammer NRW, Bonn (Agrarumweltrecht)
Rechtsanwalt/Steuerberater Ralf Stephany, Bonn (Agrarsteuerrecht)
Rechtsanwalt/Steuerberater Ralf Stephany, Bonn (Agrarsteuerrecht)
Rechtsanwalt Harald Wedemeyer, Landvolk Niedersachsen, Hannover (Öffentliches Agrarwirtschaftsrecht)
Rechtsanwalt Harald Wedemeyer, Landvolk Niedersachsen, Hannover (Öffentliches Agrarwirtschaftsrecht)
Zusendung von Manuskripten:
......@@ -54,8 +54,8 @@ Alle Artikel durchlaufen vor der Publikation ein Begutachtungsverfahren (peer re
AUR
Internet-Archiv der AUR
Autorenhinweise für Beiträge in der AUR
Internet-Archiv der AUR
Autorenhinweise für Beiträge in der AUR
Schriftleitung Zeitschrift Agrar und Umweltrecht:
......@@ -80,21 +80,21 @@ Soziale Medien
Online-Dienste
Studienangebot (eCampus)
Organisation (eCampus)
Prüfungsverwaltung (FlexNow)
Lernmanagement (Stud.IP)
Studierendenportal (eCampus)
Intranet
Stellenausschreibungen
Jobportal stellenwerk
Studienangebot (eCampus)
Organisation (eCampus)
Prüfungsverwaltung (FlexNow)
Lernmanagement (Stud.IP)
Studierendenportal (eCampus)
Intranet
Stellenausschreibungen
Jobportal stellenwerk
Service
Barrierefreiheit
Datenschutz
Kontakt
Notfall
Lageplan
Impressum
Barrierefreiheit
Datenschutz
Kontakt
Notfall
Lageplan
Impressum
......@@ -7,6 +7,9 @@ problems):
`pip install pydantic -U`
`pip install pydantic==1.10.11`
`pip install python-dotenv langchain langchain-cli openai huggingface_hub langchain_openai`
`pip install text-generation transformers numexpr langchainhub sentencepiece jinja2`
You need to copy `.env.dist` to `.env` and add values for the `OPENAI_API_KEY` and `HUGGINGFACEHUB_API_TOKEN`
emvironment variables.
\ No newline at end of file
emvironment variables.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment