instructor

Форк
0
62 строки · 2.1 Кб
1
import os
2
import instructor
3
from openai import OpenAI
4
from pydantic import BaseModel, Field
5
from typing import Optional
6
from instructor import Mode
7

8
# Extract API key from environment
9
runpod_api_key = os.environ.get("RUNPOD_API_KEY")
10
assert runpod_api_key, "RUNPOD_API_KEY is not set in environment variables"
11

12
# Base URL for OpenAI client
13
runpod_base_url = os.environ.get("RUNPOD_BASE_URL")
14
assert runpod_base_url, "RUNPOD_BASE_URL is not set in environment variables"
15

16
# Initialize OpenAI client
17
client = instructor.from_openai(
18
    OpenAI(api_key=runpod_api_key, base_url=runpod_base_url),
19
    mode=Mode.JSON,
20
)
21

22

23
data = [
24
    "Brandon is 33 years old. He works as a solution architect.",
25
    "Jason is 25 years old. He is the GOAT.",
26
    "Dominic is 45 years old. He is retired.",
27
    "Jenny is 72. She is a wife and a CEO.",
28
    "Holly is 22. She is an explorer.",
29
    "There onces was a prince, named Benny. He ruled for 10 years, which just ended. He started at 22.",
30
    "Simon says, why are you 22 years old marvin?",
31
]
32

33

34
if __name__ == "__main__":
35

36
    class UserDetail(BaseModel):
37
        name: str = Field(description="Name extracted from the text")
38
        age: int = Field(description="Age extracted from the text")
39
        occupation: Optional[str] = Field(
40
            default=None, description="Occupation extracted from the text"
41
        )
42

43
    for content in data:
44
        try:
45
            user = client.chat.completions.create(
46
                response_model=UserDetail,
47
                model="TheBloke_OpenHermes-2.5-Mistral-7B-GPTQ",
48
                messages=[
49
                    {
50
                        "role": "system",
51
                        "content": "You are an expert at outputting json. You output valid JSON.",
52
                    },
53
                    {
54
                        "role": "user",
55
                        "content": f"Extract the user details from the following text: {content}. Match your response to the following schema: {UserDetail.model_json_schema()}",
56
                    },
57
                ],
58
            )
59
            print(f"Result: {user}")
60
        except Exception as e:
61
            print(f"Error: {e}")
62
            continue
63

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.