This repository was archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
62 lines (49 loc) · 1.68 KB
/
run.py
File metadata and controls
62 lines (49 loc) · 1.68 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
import os
import requests
import json
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from dotenv import load_dotenv
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
SID_ACCESS_TOKEN = os.getenv("SID_ACCESS_TOKEN")
query_template = """
Given a writing prompt, return a query that would be useful to find relevant information.
Writing prompt:
{query}
Query:
"""
query_prompt = PromptTemplate(template=query_template, input_variables=['query'])
writing_template = """
Write a text about the following query:
{query}
Use the following context:
{context}
"""
writing_prompt = PromptTemplate(template=writing_template, input_variables=['query', 'context'])
llm = OpenAI(openai_api_key=OPENAI_API_KEY)
query_chain = LLMChain(llm=llm, prompt=query_prompt)
writing_chain = LLMChain(llm=llm, prompt=writing_prompt)
def call_sid(query: str, count: int = 5) -> list[str]:
url = 'https://api.sid.ai/v1/users/me/query'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {SID_ACCESS_TOKEN}'
}
data = {
'query': query,
'limit': count
}
response_json = requests.post(url, headers=headers, data=json.dumps(data)).json()
return [result['text'] for result in response_json['results']]
def main():
while True:
query = input('What are you writing?\n')
sid_query = query_chain.run(query)
results = call_sid(sid_query)
result_string = '\n'.join([f'{i+1}. {result}' for i, result in enumerate(results)])
output = writing_chain.run(query=query, context=result_string)
print(output)
if __name__ == '__main__':
main()