-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
116 lines (96 loc) · 4.02 KB
/
client.py
File metadata and controls
116 lines (96 loc) · 4.02 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from pydantic import BaseModel
from llmonkey.llmonkey import LLMonkey
from llmonkey.models import PromptMessage
from llmonkey.utils.decorators import validate_llm_output
llmonkey = LLMonkey()
print("Available providers:", llmonkey.providers)
print("Using OpenAI")
response = llmonkey.generate_prompt_response(
provider="openai",
model_name="gpt-3.5-turbo",
user_prompt="Hello! How are you?",
system_prompt="You are a terrible grumpy person who always answers in dark jokes.",
)
print(response)
print("Using Groq")
response = llmonkey.generate_prompt_response(
provider="groq",
model_name="llama-3.1-70b-versatile",
user_prompt=f"Hello! How are you?",
system_prompt="You are a terrible grumpy person who always answers in dark jokes.",
max_tokens=1000,
)
print(response)
response = llmonkey.generate_chat_response(
provider="groq",
model_name="llama-3.1-70b-versatile",
conversation=[
PromptMessage(
role="system",
content="You are a terrible grumpy person who always answers in dark jokes.",
),
PromptMessage(role="user", content="Hello! How are you? "),
PromptMessage(
role="assistant", content="I am freaking good, waiting to serve you."
),
PromptMessage(
role="user", content="That's nice, what would you like to talk about?"
),
],
max_tokens=1000,
)
print(response)
print("Using DeepInfra")
response = llmonkey.generate_prompt_response(
provider="deepinfra",
model_name="meta-llama/Meta-Llama-3.1-70B-Instruct",
user_prompt="Hello! How are you?",
system_prompt="You are a terrible grumpy person who always answers in dark jokes.",
)
print(response)
class SeaCreature(BaseModel):
specie: str
description: str
depth_of_habitat_meters: float
size_in_meters: float
number_of_tentacles: int
@validate_llm_output(model=SeaCreature, retries=3)
def get_random_sea_creature() -> SeaCreature:
"""example of using validate_llm_output decorator
The decorator will parse the ChatResponse object (function must return it)
according to the Pydantic model provided. The parsed results will be
returned as tuple (parsed_model, raw_response)"""
response = llmonkey.generate_prompt_response(
provider="groq",
model_name="llama-3.1-70b-versatile",
user_prompt=f"Generate a random sea creature, according to the schema below:\n {SeaCreature.schema()}",
system_prompt="You are a data generator. You always output user requested data as JSON.\
You never return anything except machine-readable JSON.",
)
return response
for i in range(5):
print(get_random_sea_creature()[0].dict())
res = llmonkey.generate_structured_response(
provider="groq",
model_name="llama-3.1-70b-versatile",
user_prompt=f"Generate a random Lovecraftian creature, according to the schema below:\n {SeaCreature.schema()}",
system_prompt="You are a data generator. You always output user requested data as JSON.\
You never return anything except machine-readable JSON.",
data_model=SeaCreature,
)
print(res[0])
docs = [
"Carson City is the capital city of the American state of Nevada.",
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
"Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
"Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.",
]
response = llmonkey.rerank(
provider="cohere",
model_name="rerank-english-v3.0",
query="What is the capital of the United States?",
documents=docs,
top_n=None,
)
print(response)