Views
No views yet
| Metric | Phi-3 Fine-tuned | Claude Sonnet 3.5 | Phi-3 (base) |
|---|---|---|---|
| Nodes Similarity | 0.78 | 0.64 | 0.64 |
| Edges Similarity | 0.49 | 0.41 | 0.30 |
| JSON Consistency | 0.99 | 0.97 | 0.96 |
| JSON Similarity | 0.75 | 0.67 | 0.63 |
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
4torch.random.manual_seed(0)
5model = AutoModelForCausalLM.from_pretrained(
6 "EmergentMethods/Phi-3-mini-4k-instruct-graph",
7 device_map="cuda",
8 torch_dtype="auto",
9 trust_remote_code=True,
10)
11
12tokenizer = AutoTokenizer.from_pretrained("EmergentMethods/Phi-3-mini-4k-instruct-graph")
13
14messages = [
15 {"role": "system", "content": """
16A chat between a curious user and an artificial intelligence Assistant. The Assistant is an expert at identifying entities and relationships in text. The Assistant responds in JSON output only.
17
18The User provides text in the format:
19
20-------Text begin-------
21<User provided text>
22-------Text end-------
23
24The Assistant follows the following steps before replying to the User:
25
261. **identify the most important entities** The Assistant identifies the most important entities in the text. These entities are listed in the JSON output under the key "nodes", they follow the structure of a list of dictionaries where each dict is:
27
28"nodes":[{"id": <entity N>, "type": <type>, "detailed_type": <detailed type>}, ...]
29
30where "type": <type> is a broad categorization of the entity. "detailed type": <detailed_type> is a very descriptive categorization of the entity.
31
322. **determine relationships** The Assistant uses the text between -------Text begin------- and -------Text end------- to determine the relationships between the entities identified in the "nodes" list defined above. These relationships are called "edges" and they follow the structure of:
33
34"edges":[{"from": <entity 1>, "to": <entity 2>, "label": <relationship>}, ...]
35
36The <entity N> must correspond to the "id" of an entity in the "nodes" list.
37
38The Assistant never repeats the same node twice. The Assistant never repeats the same edge twice.
39The Assistant responds to the User in JSON only, according to the following JSON schema:
40
41{"type":"object","properties":{"nodes":{"type":"array","items":{"type":"object","properties":{"id":{"type":"string"},"type":{"type":"string"},"detailed_type":{"type":"string"}},"required":["id","type","detailed_type"],"additionalProperties":false}},"edges":{"type":"array","items":{"type":"object","properties":{"from":{"type":"string"},"to":{"type":"string"},"label":{"type":"string"}},"required":["from","to","label"],"additionalProperties":false}}},"required":["nodes","edges"],"additionalProperties":false}
42 """},
43 {"role": "user", "content": """
44-------Text begin-------
45OpenAI is an American artificial intelligence (AI) research organization founded in December 2015 and headquartered in San Francisco, California. Its mission is to develop "safe and beneficial" artificial general intelligence, which it defines as "highly autonomous systems that outperform humans at most economically valuable work".[4] As a leading organization in the ongoing AI boom,[5] OpenAI is known for the GPT family of large language models, the DALL-E series of text-to-image models, and a text-to-video model named Sora.[6][7] Its release of ChatGPT in November 2022 has been credited with catalyzing widespread interest in generative AI.
46-------Text end-------
47"""}
48]
49
50pipe = pipeline(
51 "text-generation",
52 model=model,
53 tokenizer=tokenizer,
54)
55
56generation_args = {
57 "max_new_tokens": 500,
58 "return_full_text": False,
59 "temperature": 0.0,
60 "do_sample": False,
61}
62
63output = pipe(messages, **generation_args)
64print(output[0]['generated_text'])
65
66# Output:
67
68# {
69# "nodes": [
70# {
71# "id": "OpenAI",
72# "type": "organization",
73# "detailed_type": "ai research organization"
74# },
75# {
76# "id": "GPT family",
77# "type": "technology",
78# "detailed_type": "large language models"
79# },
80# {
81# "id": "DALL-E series",
82# "type": "technology",
83# "detailed_type": "text-to-image models"
84# },
85# {
86# "id": "Sora",
87# "type": "technology",
88# "detailed_type": "text-to-video model"
89# },
90# {
91# "id": "ChatGPT",
92# "type": "technology",
93# "detailed_type": "generative ai"
94# },
95# {
96# "id": "San Francisco",
97# "type": "location",
98# "detailed_type": "city"
99# },
100# {
101# "id": "California",
102# "type": "location",
103# "detailed_type": "state"
104# },
105# {
106# "id": "December 2015",
107# "type": "date",
108# "detailed_type": "foundation date"
109# },
110# {
111# "id": "November 2022",
112# "type": "date",
113# "detailed_type": "release date"
114# }
115# ],
116# "edges": [
117# {
118# "from": "OpenAI",
119# "to": "San Francisco",
120# "label": "headquartered in"
121# },
122# {
123# "from": "San Francisco",
124# "to": "California",
125# "label": "located in"
126# },
127# {
128# "from": "OpenAI",
129# "to": "December 2015",
130# "label": "founded in"
131# },
132# {
133# "from": "OpenAI",
134# "to": "GPT family",
135# "label": "developed"
136# },
137# {
138# "from": "OpenAI",
139# "to": "DALL-E series",
140# "label": "developed"
141# },
142# {
143# "from": "OpenAI",
144# "to": "Sora",
145# "label": "developed"
146# },
147# {
148# "from": "OpenAI",
149# "to": "ChatGPT",
150# "label": "released"
151# },
152# {
153# "from": "ChatGPT",
154# "to": "November 2022",
155# "label": "released in"
156# }
157# ]
158# }