File size: 1,502 Bytes
ec99d5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import json
from groq import AsyncGroq

groq_client = AsyncGroq(api_key=os.getenv("GROQ_API_KEY", ""))

async def extract_citations(text: str) -> dict:
    """
    Uses LLaMA3-70B zero-shot to extract and format citations from raw text.
    """
    # Only check the last 4000 characters where references usually are
    text_snippet = text[-4000:] if len(text) > 4000 else text
    
    prompt = f"""
    Tugas Anda adalah mengekstrak daftar pustaka/referensi dari teks di bawah ini.
    Format output HARUS berupa JSON object dengan key 'citations' yang berisi array of objects.
    Setiap object memiliki kunci:
    - 'author': nama pengarang
    - 'year': tahun publikasi
    - 'title': judul jurnal/buku
    - 'venue': nama jurnal atau penerbit (opsional)
    
    Jika tidak ada daftar pustaka yang ditemukan, kembalikan {{"citations": []}}.
    Hanya kembalikan JSON murni, tanpa penjelasan apapun.
    
    Teks:
    {text_snippet}
    """
    
    try:
        response = await groq_client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.1,
            max_tokens=1024,
            response_format={"type": "json_object"}
        )
        
        content = response.choices[0].message.content
        data = json.loads(content)
        return {"success": True, "data": data.get("citations", [])}
    except Exception as e:
        return {"success": False, "error": str(e)}