-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGemini_api.py
93 lines (76 loc) · 3.12 KB
/
Gemini_api.py
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
from typing import List
from google.api_core.client_options import ClientOptions
from google.cloud import discoveryengine_v1 as discoveryengine
# TODO(developer): Uncomment these variables before running the sample.
project_id = "your project ID"
location = "global" # Values: "global", "us", "eu"
data_store_id = "your data store"
def multi_turn_search_sample(
project_id: str,
location: str,
data_store_id: str,
) -> None:
client_options = (
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
if location != "global"
else None
)
# Create a client
client = discoveryengine.ConversationalSearchServiceClient(
client_options=client_options
)
# Initialize Multi-Turn Session
parent = f"projects/{project_id}/locations/{location}/dataStores/{data_store_id}"
conversation = client.create_conversation(
parent=parent,
conversation=discoveryengine.Conversation(),
)
while True:
search_query = input("Enter your query (or type 'exit' to end): ")
if search_query.lower() == 'exit':
break
# Add new message to session
request = discoveryengine.ConverseConversationRequest(
name=conversation.name,
query=discoveryengine.TextInput(input=search_query),
serving_config=f"{parent}/servingConfigs/default_config",
summary_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec(
summary_result_count=3,
include_citations=True,
),
)
response = client.converse_conversation(request)
print(f"Reply: {response.reply.summary.summary_text}\n")
# Function to initialize the client and conversation
def initialize_conversation(project_id: str, location: str, data_store_id: str):
client_options = (
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
if location != "global"
else None
)
client = discoveryengine.ConversationalSearchServiceClient(
client_options=client_options
)
parent = f"projects/{project_id}/locations/{location}/dataStores/{data_store_id}"
conversation = client.create_conversation(
parent=parent,
conversation=discoveryengine.Conversation(),
)
return client, conversation.name, parent
# Function to handle conversation
def converse_conversation(client, conversation_name, parent, search_query):
request = discoveryengine.ConverseConversationRequest(
name=conversation_name,
query=discoveryengine.TextInput(input=search_query),
serving_config=f"{parent}/servingConfigs/default_config",
summary_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec(
summary_result_count=3,
include_citations=True,
model_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec.ModelSpec(
version="gemini-1.0-pro-002/answer_gen/v1",
),
),
)
response = client.converse_conversation(request)
return response
#multi_turn_search_sample(project_id, location, data_store_id)