-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
79 lines (66 loc) · 2.34 KB
/
model.py
File metadata and controls
79 lines (66 loc) · 2.34 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
from langchain_community.chat_models import ChatOpenAI
from typing import Optional, Any
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv("OPENROUTER_API_KEY")
class ChatModel(ChatOpenAI):
"""
Creates a chat model from openrouter.ai using the OpenAI API
"""
def __init__(
self,
model_name: str,
openai_api_key: Optional[str] = None,
openai_api_base: str="https://openrouter.ai/api/v1",
**kwargs: Any):
openai_api_key = openai_api_key or os.getenv('OPENROUTER_API_KEY')
super().__init__(
openai_api_base=openai_api_base,
openai_api_key=openai_api_key,
model_name=model_name,
**kwargs
)
def get_model(model_name: str = "google/gemma-3-27b-it:free") -> ChatModel:
"""
Gets a reference to a model
:param model_name: Name of the model
:type model_name: str
:return: the model
:rtype: ChatModel
"""
return ChatModel(
model_name=model_name,
max_tokens=512,
temperature=0
)
if __name__ == "__main__":
model = get_model()
from langchain_core.messages import SystemMessage, HumanMessage
from langchain.prompts import ChatPromptTemplate
prompt_template = ChatPromptTemplate([
("human", "You are a helpful assistant"),
("human", "What is {playwright}'s most recent play")
])
response = model.invoke(
[HumanMessage("You are a helpful assistant."),
HumanMessage("What are some plays by Tawfiq al-Hakim?")])
print(response.content)
print("----------")
response = model.invoke(
[HumanMessage("You are a helpful assistant."),
HumanMessage("What is Ryan Calais Camerons's most recent play?")])
print(response.content)
print("----------")
response = model.invoke(
[HumanMessage("You are a helpful assistant."),
HumanMessage("What Broadway shows have more than 10,000 performances?")])
print(response.content)
print(prompt_template.invoke({"playwright": "Ryan Calais Cameron"}))
response = model.invoke(prompt_template.invoke({"playwright": "Ryan Calais Cameron"}))
print(response.content)
chain = prompt_template | model
response = chain.invoke(
{"playwright": "Ryan Calais Cameron"})
print(response)
pass