File size: 1,886 Bytes
0ebb816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os

import gradio as gr
from openai import OpenAI
import logging
import anthropic

logging.basicConfig(level=logging.INFO)
logging.getLogger("gradio").setLevel(logging.INFO)
logging.getLogger("httpx").setLevel(logging.WARNING)


client = OpenAI()


def generate_completion(input, history):

    messages = [
        {
            "role": "system",
            "content": "You are a world-class extractor of information from messy job postings.",
        }
    ]

    # Convert history from a list of lists to a list of dictionaries
    if history:
        for entry in history:
            # Assuming each entry has exactly 2 elements: user input and assistant response
            if len(entry) == 2:  # Validate format
                # Append user message
                messages.append(
                    {
                        "role": "user",
                        "content": entry[0],
                    }
                )
                # Append assistant response
                messages.append(
                    {
                        "role": "assistant",
                        "content": entry[1],
                    }
                )

    # Append the current user message
    messages.append(
        {
            "role": "user",
            "content": input,
        }
    )

    response = client.chat.completions.create(
        model="gpt-3.5-turbo-0125",
        messages=messages,  # type: ignore
        stream=True,
        temperature=0,
        max_tokens=4000,
    )  # type: ignore

    answer_str: str = ""
    for chunk in response:
        if chunk.choices[0].delta.content is not None:
            answer_str += chunk.choices[0].delta.content
        else:
            answer_str += ""
        yield answer_str


if __name__ == "__main__":
    demo = gr.ChatInterface(fn=generate_completion)
    demo.queue()
    demo.launch()